<?php

/**
 * 俱乐部相关
 */
class cupModel extends paomeBaseModel
{
    const USER_ONCE_MIN_MILEAGE = 3;//用户单次上传公里数下限
    const USER_ONCE_MAX_SPEED   = 600;//单位：分钟
    const USER_DAY_MAX_MILEAGE  = 45;//用户一天上传公里数上限
    const CLUB_SPEED_MIN_USER_NUM = 5;//战队计算配速最小跑步人数下限
    const DEFAULT_SPEED_RANK_VALUE = 10000000;//配速默认值
    //test
    //const USER_ONCE_MIN_MILEAGE = 0.05;//用户单次上传公里数下限
    //const USER_DAY_MAX_MILEAGE  = 5;//用户一天上传公里数上限
    //const CLUB_SPEED_MIN_USER_NUM = 2;//战队计算配速最小跑步人数下限

    public function __construct(& $base)
    {
        parent::__construct($base);

        $this->table = 'r_pm_contest';
    }

    /**
     * 查询赛季列表
     */
    public function getPaomeList(array $params) {
        $start     = $params['start'] ? (int)$params['start'] : 0;
        $limitNum  = $params['limit'] ? (int)$params['limit'] : 10;
        $title     = isset($params['name']) ? trim($params['name']) : '';
        $status    = isset($params['status']) ? (int)$params['status'] : 0;
        $where = '1=1';
        if ($title) {
            $where .=' and title like '."'%$title%'";
        }
        if ($status) {
            $where .=' and status ='.$status;
        }

        $sql ="select * from r_pm_contest where $where order by id desc limit $start,$limitNum";
        $cntsql ="select count(1) as cnt from r_pm_contest where $where";
        $this->total =  $this->paomeDb->getOne("$cntsql");
        return $this->paomeDb->getAll($sql);
    }

    /**
     * 赛季轮次列表
     */
    public function getPaomeRoundsList(array $params) {
        $paomeId = $params['paomeId'];
        $sql = 'select * from r_pm_rounds where contest_id='.$paomeId.' and status=0 order by type';
        return $this->paomeDb->getAll($sql);
    }

    /**
     * 赛季信息
     */
    public function getPaomeInfo($paomeId,$fields='*') {
        return  $this->paomeDb->getRow("select $fields from r_pm_contest where id=$paomeId");
    }

    /**
     * 查询赛季时间是否跟其他赛季交叉
     */
    public function getPaomeIsRepeatTime($signTime,$endTime) {
       return  $this->paomeDb->getOne("select id from r_pm_contest where $signTime <= end_time and $endTime >=sign_time");
    }

    /**
     * 增加赛季
     */
    public function addPaome(array $params) {
        //$id = (int)$params['id'];
        $title = $params['title'];
        $image = $params['image'];
        $signTime = $params['signTime'];
        $startTime = $params['startTime'];
        $endTime = $params['endTime'];
        $minNum = (int)$params['minNum'];
        $maxNum = (int)$params['maxNum'];
        $imageText = $params['imageText'];
        $rate = $params['rate'];
        $rateType = $params['rateType'];
        $introduce = $params['introduce'];
        $type = $this->paomeDb->getOne("select type from r_pm_contest order by type desc limit 1");
        $data = array(
            'title'=>$title,
            'sign_time'=>strtotime($signTime),
            'start_time'=>strtotime($startTime),
            'end_time'=>strtotime($endTime),
            'min_num'=>$minNum,
            'max_num'=>$maxNum,
            'image'=>$image,
            'image_text'=>$imageText,
            'rate'=>$rate,
            'rate_type'=>$rateType,
            'introduce'=>$introduce,
            'type'=>$type+1
        );

        $data['status'] = 0;
        $data['create_time'] = time();
        $result = $this->paomeDb->insertRow('r_pm_contest',$data,mysqldb::INSERT_COMMON);
        return $result;
    }

    /**
     * 增加赛季轮次
     */
    public function addPaomeRounds($paomeId,array $params) {
        $data = array(
            'contest_id'=>$paomeId,
            'start_time'=>strtotime($params['startTime']),
            'end_time'=>strtotime($params['endTime']),
            'type'=>$params['type'],
            'create_time'=>time(),
        );
        $result = $this->paomeDb->insertRow('r_pm_rounds',$data,mysqldb::INSERT_COMMON);
        return $result;
    }

    /**
     *  查询当前的赛季信息
     */
    public function getCurrentPaomeInfo($field='*') {
//        //当前的时间戳
//        $nowTime = time();
//        $sql = "select * from r_pm_rounds where start_time <= $nowTime and end_time >= $nowTime and status = 0    limit 1";
//        $roundInfo = $this->paomeDb->getRow($sql);
//        if (!$roundInfo) {
//            return  [];
//        }
        $sql = "select $field from r_pm_contest where flag=1";
        $info = $this->paomeDb->getRow($sql);
        return $info ? $info : [];
    }

    /*************************************定时器********************************/

    /**
     * 赛季状态自动变更
     */
    public function autoModifyPaomeStatus() {
        //查询所有赛季
        $sql = "select * from r_pm_contest where flag=1 and status<120 and status>=0 order by sign_time asc limit 1";
        $info = $this->paomeDb->getRow($sql);
        if (!$info) {
            return false;
        }
        $time = time();
        $logPath = $this->base->config['log-dir'].'api/paomeStatuss-'.date('Y-m-d').'.log';
        error_log('时间'.date('Y-m-d H:i:s').' start赛季状态自动变更:$info'.var_export($info,true).PHP_EOL,3,$logPath);
        if ($info['sign_time']  > $time){
            error_log('时间'.date('Y-m-d H:i:s').' 赛季未开始:)'.PHP_EOL,3,$logPath);
            return false;//0：赛季未开始
        }
        if ($info['end_time']  <= $time){ //已结束
            error_log('时间'.date('Y-m-d H:i:s').' 赛季已结束:'.PHP_EOL,3,$logPath);
            $type = 120;
           /*争霸赛积分新规 先完成积分同步，再完成证书积分写入*/
            $rankCronAutoInitIsExistKey = 'rankIsExistCronAutoInit-'.$info['id'];
            $date = date('Ymd');
            $value = $this->redis->zScore($rankCronAutoInitIsExistKey, $date);
            if (!$value) {
                error_log('时间'.date('Y-m-d H:i:s').' 冠军赛120状态变更-当日数据初始化未完成-下一分钟再循环'.PHP_EOL,3,$logPath);
                return false;
            } else {
                error_log('时间'.date('Y-m-d H:i:s').' 冠军赛120状态变更-当日数据初始化已完成'.PHP_EOL,3,$logPath);
            }
        } else {
            $sql = "select `type` from r_pm_rounds where contest_id={$info['id']} and status=0 and $time >=start_time and $time<=end_time";
            $roundsinfo = $this->paomeDb->getRow($sql);
            $type = $roundsinfo['type'];
            error_log('时间'.date('Y-m-d H:i:s').' 赛季状态自动变更:$roundsinfo'.var_export($roundsinfo,true).PHP_EOL,3,$logPath);
            if(!$roundsinfo) {
                return false;
            }
        }
        if ($info['status'] <> $type) {
            //赛季状态变更
            $r = $this->saveContestStatus($info['id'],$type);
            //俱乐部资格状态筛选变更
            $this->modifyClubStatusForContest($info,$type);
            error_log('时间'.date('Y-m-d H:i:s').' OK状态变更-当日数据已完成状态变更：type:'.$type.PHP_EOL,3,$logPath);
        } else {
            error_log('时间'.date('Y-m-d H:i:s').' Repeat状态变更-当日数据已完成状态变更：type:'.$type.PHP_EOL,3,$logPath);
        }
    }

    /**
     * ①更新赛季状态
     */
    public function saveContestStatus($paomeId,$status) {
        $sql = "update r_pm_contest set status = $status where id=$paomeId";
        return $this->paomeDb->update($sql);
    }

    /**
     * ②俱乐部竞赛状态变更
     * @param $val  赛季info
     */
    public function modifyClubStatusForContest($val,$modifyType) {
        $logPath = $this->base->config['log-dir'].'api/paomeStatuss-'.date('Y-m-d').'.log';
        switch($modifyType) {
            case 1:
                error_log('时间'.date('Y-m-d H:i:s').' ②俱乐部竞赛状态变更:$modifyType:1'.PHP_EOL,3,$logPath);
                break;
            case 5://第一轮报名俱乐部筛选
                error_log('时间'.date('Y-m-d H:i:s').' ②俱乐部竞赛状态变更:$modifyType:5'.PHP_EOL,3,$logPath);
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=1
                                              and contest_id={$val['id']}
                                              and status=0");
                if (!$list) return false;
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('status'=>5);
                    } else {
                        $data = array('status'=>15);//参与成功
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    error_log('时间'.date('Y-m-d H:i:s').' ②俱乐部竞赛状态变更:$sql:'.$sql.' $arrResult'.var_export($arrResult,true).PHP_EOL,3,$logPath);
                    $r = $this->paomeDb->update($sql);
                    if ($r && ($data['status'] == 15)) {//初始化俱乐部轮次表
                        error_log('时间'.date('Y-m-d H:i:s').' ②俱乐部竞赛状态变更:初始化俱乐部轮次表'.PHP_EOL,3,$logPath);
                        $arrClubRounds = $this->paomeDb->getOne("select id from r_pm_club_rounds
                                                    where contest_id={$val['id']}
                                                    and club_id={$v['club_id']}
                                                    and type=1");
                        if (!$arrClubRounds) {
                            $data = array(
                                'contest_id'=>$val['id'],
                                'club_id'=>$v['club_id'],
                                'create_time'=>time(),
                                'type'=>1
                            );
                            $r = $this->paomeDb->insertRow('r_pm_club_rounds',$data,0);
                            error_log('时间'.date('Y-m-d H:i:s').' ②俱乐部竞赛状态变更:初始化俱乐部轮次表 $data'.var_export($data,true).PHP_EOL,3,$logPath);
                        }
                    }
                }

                //r_pm_club_status_list 轮次阶段信息更新
                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=1
                                              and contest_id={$val['id']}
                                              and status=0");
                foreach ($list as $k=>$v) {
                    $data = isset($arrResult[$v['club_id']]) ? $arrResult[$v['club_id']] : [];
                    if (!$data) continue;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                    error_log('时间'.date('Y-m-d H:i:s').' ②俱乐部竞赛状态变更:轮次阶段信息更新 sql:'.$sql.PHP_EOL,3,$logPath);
                    $r = $this->paomeDb->update($sql);
                }
                break;
            case 6://第一轮预赛结束空档期(22:00-24:00)
                error_log('时间'.date('Y-m-d H:i:s').' 时间'.date('Y-m-d H:i:s').' 第一轮预赛结束空档期(22:00-24:00):$modifyType:6'.PHP_EOL,3,$logPath);
                $sql = "update r_pm_club_status set status=20 where contest_id={$val['id']} and type=1 and status=15";
                $this->paomeDb->update($sql);
                $sql = "update r_pm_club_status_list set status=20 where contest_id={$val['id']} and type=1 and status=15";
                $this->paomeDb->update($sql);
                break;
            case 10://第一轮决赛俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=1
                                              and contest_id={$val['id']}
                                              and status=20");

                if (!$list) break;
                //删除淘汰战队前 先同步排名 todo

                //查询全部跑量排名,为了正确的同步淘汰的战队排名
                $arrRank = [];
                foreach ($list as $k2=>$v2) {
                    $rankNumNew = $this->base->load('paome/rank')->getClubRankNum($v2['club_id'],$val['id'],1);//第一轮预赛
                    $arrRank[$v2['club_id']] = $rankNumNew;
                }
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    $rankNum = $arrRank[$v['club_id']];//第一轮预赛
                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('type'=>1,'status'=>10);//冠军赛人数不满 ，算作状态：10未能晋级冠军赛  来处理
                        error_log('时间'.date('Y-m-d H:i:s').' 第一轮冠军赛1:$rankNum:'. var_export($rankNum, true).' 百分比'.var_export($rankNum/count($list)*100,true).PHP_EOL,3,$logPath);

                    } else if ($rankNum == null || ($rankNum/count($list)*100)>$val['rate']) { //跑量70%进入决赛
                        $data = array('type'=>1,'status'=>10);//淘汰：排名前70%
                        error_log('时间'.date('Y-m-d H:i:s').' 第一轮冠军赛2:$rankNum:'. var_export($rankNum, true).' 百分比'.var_export($rankNum/count($list)*100,true).' rate:'.var_export($val['rate'],true).PHP_EOL,3,$logPath);
                    }
                    else if(($rankNum/count($list)*100)<=$val['rate']){
                        $data = array('type'=>5,'status'=>15);//参与成功
                        error_log('时间'.date('Y-m-d H:i:s').' 第一轮冠军赛3:$rankNum:'. var_export($rankNum, true).' 百分比'.var_export($rankNum/count($list)*100,true).' rate:'.var_export($val['rate'],true).PHP_EOL,3,$logPath);

                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                    if ($r && ($data['status']==10)) {//维度：仅跑量，规则:淘汰俱乐部退出当前轮次排名，加入到淘汰redis列表
                        $paomeId = $val['id'];
                        $roundId = 1;
                        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
                        $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到

                        $failMile = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
                        //写入俱乐部淘汰表
                        $this->base->load('paome/paomeClub')->addClubOver(
                            array(
                                'paomeId'=>$paomeId,
                                'clubId'=>$v['club_id'],
                                'roundId2'=>1,//止步于第一轮预赛
                                'historyRank'=>$rankNum,
                                'mileage'=>(double)$failMile,
                            )
                        );
                        $this->redis->zRem($rankClubKeyMile,$v['club_id']);
                        $this->redis->zRem($rankClubKeyMileOk,$v['club_id']);
                    }

                    if ($r && ($data['status']==15)) {//晋级冠军赛新增list
                        $data2 = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'create_time'=>time(),
                            'type'=>$data['type'],
                            'status'=>15,
                            'mileage'=>$v['mileage'],
                            'speed'=>$v['speed'],
                            'support_num'=>$v['support_num'],
                            'energy_num'=>$v['energy_num'],
                            'agree_num'=>$v['agree_num'],
                            'elapsedtime'=>$v['elapsedtime']
                        );
                        if (!$id = $this->paomeDb->getOne("select id from r_pm_club_status_list where contest_id={$val['id']} and club_id={$v['club_id']} and `type`={$data['type']}")) {
                            $this->paomeDb->insertRow('r_pm_club_status_list',$data2,0);
                        }else {
                            $setStr = common::arrayToString($data2);
                            $sql = "update r_pm_club_status_list set $setStr where id= $id";
                            $this->paomeDb->update($sql);
                        }
                    }
                }
                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=1
                                              and contest_id={$val['id']}
                                              and status=20");
                foreach ($list as $k=>$v) {
                    $data = isset($arrResult[$v['club_id']]) ? $arrResult[$v['club_id']] : [];
                    if (!$data) continue;
                    if ($data['status'] == 15) {
                        continue;
                    } else {
                        $setStr = common::arrayToString($data);
                        $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                        $this->paomeDb->update($sql);
                    }
                }
                break;
            case 11://第一轮决赛结束空档期(22:00-24:00)
                $sql = "update r_pm_club_status set status=20 where contest_id={$val['id']} and type=5 and status=15";
                $this->paomeDb->update($sql);
                $sql = "update r_pm_club_status_list set status=20 where contest_id={$val['id']} and type=5 and status=15";
                $this->paomeDb->update($sql);
                //通知用户获得证书

                break;
            case 12://第一轮冠军俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=5
                                              and contest_id={$val['id']}
                                              and status=20");
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    $rankNum = $this->base->load('paome/rank')->getClubRankNum($v['club_id'],$val['id'],1);//第一轮预赛

                    /*发证书start*/
                    $paomeId = $val['id'];
                    $roundId = 1;
                    $typeWd = 1;//里程
                    $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
                    $rankValue = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
                    $this->base->load('paome/paomeClub')->sendCertificateToClub($val['id'], $v['club_id'], $roundId, $typeWd, (int)$rankNum, (double)$rankValue);
                    /*发证书end*/

                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('type'=>5,'status'=>10);
                    } else if ($rankNum == null || $rankNum>10) { //跑量10进入冠军争霸赛
                        //$data = array('type'=>5,'status'=>10);//淘汰：排名大于10
                        continue;
                    } else {
                        //增加争霸赛资格用户记录
                        $data = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'create_time'=>time(),
                            'type'=>0,
                            'round_type'=>1
                        );
                        $r = $this->paomeDb->insertRow('r_pm_champions_member',$data,0);
                        //增加争霸赛资格redis记录
                        $rankClubKeyMileZbsTicket = 'rankClub-mileageZbs-ticker-'.$val['id'].'-1';//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
                        $this->redis->hSet($rankClubKeyMileZbsTicket,$v['club_id'],1);
                        continue;
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                    if ($r && ($data['status']==10)) {//维度：仅跑量，规则:淘汰俱乐部退出当前轮次排名，加入到淘汰redis列表
                        $paomeId = $val['id'];
                        $roundId = 1;
//                        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
//                        $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
//
//                        $failMile = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
//                        $failMileRank = $this->redis->zRevRank($rankClubKeyMile,$v['club_id']);
//                        //写入俱乐部淘汰表
//                        $this->base->load('paome/paomeClub')->addClubOver(
//                            array(
//                                'paomeId'=>$paomeId,
//                                'clubId'=>$v['club_id'],
//                                'roundId2'=>5,//止步于第一轮冠军赛
//                                'historyRank'=>$failMileRank!==false ? $failMileRank+1 : 0,
//                                'mileage'=>(double)$failMile,
//                            )
//                        );
//                        $this->redis->zRem($rankClubKeyMile,$v['club_id']);
//                        $this->redis->zRem($rankClubKeyMileOk,$v['club_id']);
                    }
                }

                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=5
                                              and contest_id={$val['id']}
                                              and status=20");
                foreach ($list as $k=>$v) {
                    $data = isset($arrResult[$v['club_id']]) ? $arrResult[$v['club_id']] : [];
                    if (!$data) continue;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                    $this->paomeDb->update($sql);
                }
                break;
            case 15://第二轮预赛已报名俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=10
                                              and contest_id={$val['id']}
                                              and status=0");
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('status'=>5);
                    } else {
                        $data = array('status'=>15);//参与成功
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                    if ($r && ($data['status'] == 15)) {//初始化俱乐部轮次表
                        $arrClubRounds = $this->paomeDb->getOne("select id from r_pm_club_rounds
                                                    where contest_id={$val['id']}
                                                    and club_id={$v['club_id']}
                                                    and type=5");
                        if (!$arrClubRounds) {
                            $data = array(
                                'contest_id'=>$val['id'],
                                'club_id'=>$v['club_id'],
                                'create_time'=>time(),
                                'type'=>5
                            );
                            $r = $this->paomeDb->insertRow('r_pm_club_rounds',$data,0);
                        }
                    }
                }

                //r_pm_club_status_list 轮次阶段信息更新
                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=10
                                              and contest_id={$val['id']}
                                              and status=0");
                foreach ($list as $k=>$v) {
                    $data = isset($arrResult[$v['club_id']]) ? $arrResult[$v['club_id']] : [];
                    if (!$data) continue;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                }
                break;
            case 16://第二轮预赛结束空档期(22:00-24:00)
                $sql = "update r_pm_club_status set status=20 where contest_id={$val['id']} and type=10 and status=15";
                $this->paomeDb->update($sql);
                $sql = "update r_pm_club_status_list set status=20 where contest_id={$val['id']} and type=10 and status=15";
                $this->paomeDb->update($sql);
                break;
            case 20://第二轮决赛俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=10
                                              and contest_id={$val['id']}
                                              and status=20");
                //删除淘汰战队前 先同步排名  todo

                //查询全部跑量排名,为了正确的同步淘汰的战队排名
                $arrRank = [];
                foreach ($list as $k2=>$v2) {
                    $rankNumNew = $this->base->load('paome/rank')->getClubRankNum($v2['club_id'],$val['id'],5);//第二轮预赛数据
                    $arrRank[$v2['club_id']] = $rankNumNew;
                }
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    $rankNum = $arrRank[$v['club_id']];//第二轮预赛数据
                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('type'=>10,'status'=>10); //冠军赛人数不满 ，算作状态：10未能晋级冠军赛  来处理
                    } else if ($rankNum == null || ($rankNum/count($list)*100)>$val['rate']) { //跑量70%进入决赛
                        $data = array('type'=>10,'status'=>10);//淘汰：排名前70%
                    }
                    else{
                        $data = array('type'=>15,'status'=>15);//参与成功
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                    if ($r && ($data['status']==10)) {//维度：仅跑量，规则:淘汰俱乐部退出当前轮次排名，加入到淘汰redis列表
                        $paomeId = $val['id'];
                        $roundId = 5;
                        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
                        $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到

                        $failMile = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
                        //写入俱乐部淘汰表
                        $this->base->load('paome/paomeClub')->addClubOver(
                            array(
                                'paomeId'=>$paomeId,
                                'clubId'=>$v['club_id'],
                                'roundId2'=>10,//止步于第二轮预赛
                                'historyRank'=>$rankNum,
                                'mileage'=>(double)$failMile,
                            )
                        );
                        $this->redis->zRem($rankClubKeyMile,$v['club_id']);
                        $this->redis->zRem($rankClubKeyMileOk,$v['club_id']);
                    }
                    if ($r && ($data['status']==15)) {//晋级冠军赛新增list
                        $data2 = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'create_time'=>time(),
                            'type'=>$data['type'],
                            'status'=>15,
                            'mileage'=>$v['mileage'],
                            'speed'=>$v['speed'],
                            'support_num'=>$v['support_num'],
                            'energy_num'=>$v['energy_num'],
                            'agree_num'=>$v['agree_num'],
                            'elapsedtime'=>$v['elapsedtime']
                        );
                        if (!$id = $this->paomeDb->getOne("select id from r_pm_club_status_list where contest_id={$val['id']} and club_id={$v['club_id']} and `type`={$data['type']}")) {
                            $this->paomeDb->insertRow('r_pm_club_status_list',$data2,0);
                        }else {
                            $setStr = common::arrayToString($data2);
                            $sql = "update r_pm_club_status_list set $setStr where id= $id";
                            $this->paomeDb->update($sql);
                        }
                    }
                }
                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=10
                                              and contest_id={$val['id']}
                                              and status=20");
                foreach ($list as $k=>$v) {
                    $data = $arrResult[$v['club_id']];
                    if (!$data) continue;
                    if ($data['status'] == 15) {
                        continue;
                    } else {
                        $setStr = common::arrayToString($data);
                        $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                        $this->paomeDb->update($sql);
                    }

                }
                break;
            case 21://第二轮决赛结束空档期(22:00-24:00)
                $sql = "update r_pm_club_status set status=20 where contest_id={$val['id']} and `type`=15 and status=15";
                $this->paomeDb->update($sql);
                $sql = "update r_pm_club_status_list set status=20 where contest_id={$val['id']} and `type`=15 and status=15";
                $this->paomeDb->update($sql);
                //通知获得证书
                break;
            case 22://第二轮冠军俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=15
                                              and contest_id={$val['id']}
                                              and status=20");
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    $rankNum = $this->base->load('paome/rank')->getClubRankNumOk($v['club_id'],$val['id'],5);//第二轮预赛

                    $rankNumOld = $this->base->load('paome/rank')->getClubRankNum($v['club_id'],$val['id'],5);//第二轮预赛
                    /*发证书start*/
                    $paomeId = $val['id'];
                    $roundId = 5;
                    $typeWd = 1;//里程
                    $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
                    $rankValue = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
                    $this->base->load('paome/paomeClub')->sendCertificateToClub($val['id'], $v['club_id'], $roundId, $typeWd, (int)$rankNumOld, (double)$rankValue);
                    /*发证书end*/

                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('type'=>15,'status'=>10);
                    } else if ($rankNum == null || $rankNum>10) { //跑量10进入冠军争霸赛
                        continue;
                        //$data = array('type'=>15,'status'=>10);//淘汰：排名大于10
                    } else {
                        //增加争霸赛资格用户记录
                        $data = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'create_time'=>time(),
                            'type'=>0,
                            'round_type'=>5
                        );
                        $r = $this->paomeDb->insertRow('r_pm_champions_member',$data,0);
                        //增加争霸赛资格redis记录
                        $rankClubKeyMileZbsTicket = 'rankClub-mileageZbs-ticker-'.$val['id'].'-5';//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
                        $this->redis->hSet($rankClubKeyMileZbsTicket,$v['club_id'],1);
                        continue;
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
//                    if ($r && ($data['status']==10)) {//维度：仅跑量，规则:淘汰俱乐部退出当前轮次排名，加入到淘汰redis列表
//                        $paomeId = $val['id'];
//                        $roundId = 5;
//                        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
//                        $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
//
//                        $failMile = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
//                        $failMileRank = $this->redis->zRevRank($rankClubKeyMile,$v['club_id']);
//                        //写入俱乐部淘汰表
//                        $this->base->load('paome/paomeClub')->addClubOver(
//                            array(
//                                'paomeId'=>$paomeId,
//                                'clubId'=>$v['club_id'],
//                                'roundId2'=>15,//止步于第二轮冠军赛
//                                'historyRank'=>$failMileRank!==false ? $failMileRank+1 : 0,
//                                'mileage'=>(double)$failMile,
//                            )
//                        );
//                        $this->redis->zRem($rankClubKeyMile,$v['club_id']);
//                        $this->redis->zRem($rankClubKeyMileOk,$v['club_id']);
//                    }
                }

                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=15
                                              and contest_id={$val['id']}
                                              and status=20");
                foreach ($list as $k=>$v) {
                    $data = $arrResult[$v['club_id']];
                    if (!$data) continue;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                    $this->paomeDb->update($sql);
                }
                break;
            case 25://第三轮报名俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=20
                                              and contest_id={$val['id']}
                                              and status=0");
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('status'=>5);
                    } else {
                        $data = array('status'=>15);//参与成功
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                    if ($r && ($data['status'] == 15) ) {//初始化俱乐部轮次表
                        $arrClubRounds = $this->paomeDb->getOne("select id from r_pm_club_rounds
                                                    where contest_id={$val['id']}
                                                    and club_id={$v['club_id']}
                                                    and type=10");
                        if (!$arrClubRounds) {
                            $data = array(
                                'contest_id'=>$val['id'],
                                'club_id'=>$v['club_id'],
                                'create_time'=>time(),
                                'type'=>10
                            );
                            $r = $this->paomeDb->insertRow('r_pm_club_rounds',$data,0);
                        }
                    }
                }

                //r_pm_club_status_list 轮次阶段信息更新
                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=20
                                              and contest_id={$val['id']}
                                              and status=0");
                foreach ($list as $k=>$v) {
                    $data = $arrResult[$v['club_id']];
                    if (!$data) continue;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                }
                break;
            case 26://第三轮预赛结束空档期(22:00-24:00)
                $sql = "update r_pm_club_status set status=20 where contest_id={$val['id']} and type=20 and status=15";
                $this->paomeDb->update($sql);
                $sql = "update r_pm_club_status_list set status=20 where contest_id={$val['id']} and type=20 and status=15";
                $this->paomeDb->update($sql);
                break;
            case 30://第三轮决赛俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=20
                                              and contest_id={$val['id']}
                                              and status=20");
                //删除淘汰战队前 先同步排名 todo

                //查询全部跑量排名,为了正确的同步淘汰的战队排名
                $arrRank = [];
                foreach ($list as $k2=>$v2) {
                    $rankNumNew = $this->base->load('paome/rank')->getClubRankNum($v2['club_id'],$val['id'],10);//第三轮预赛数据
                    $arrRank[$v2['club_id']] = $rankNumNew;
                }
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    $rankNum =  $arrRank[$v['club_id']];//第三轮预赛数据
                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('type'=>20,'status'=>10);//冠军赛人数不满 ，算作状态：10未能晋级冠军赛  来处理
                    } else if ($rankNum == null || ($rankNum/count($list)*100)>$val['rate']) { //跑量70%进入决赛
                        $data = array('type'=>20,'status'=>10);//淘汰：排名前70%
                    }
                    else{
                        $data = array('type'=>25,'status'=>15);//参与成功
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
                    if ($r && ($data['status']==10)) {//维度：仅跑量，规则:淘汰俱乐部退出当前轮次排名，加入到淘汰redis列表
                        $paomeId = $val['id'];
                        $roundId = 10;
                        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
                        $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到

                        $failMile = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
                        //写入俱乐部淘汰表
                        $this->base->load('paome/paomeClub')->addClubOver(
                            array(
                                'paomeId'=>$paomeId,
                                'clubId'=>$v['club_id'],
                                'roundId2'=>20,//止步于第三轮预赛
                                'historyRank'=>$rankNum,
                                'mileage'=>(double)$failMile,
                            )
                        );
                        $this->redis->zRem($rankClubKeyMile,$v['club_id']);
                        $this->redis->zRem($rankClubKeyMileOk,$v['club_id']);
                    }
                    if ($r && ($data['status']==15)) {//晋级冠军赛新增list
                        $data2 = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'create_time'=>time(),
                            'type'=>$data['type'],
                            'status'=>15,
                            'mileage'=>$v['mileage'],
                            'speed'=>$v['speed'],
                            'support_num'=>$v['support_num'],
                            'energy_num'=>$v['energy_num'],
                            'agree_num'=>$v['agree_num'],
                            'elapsedtime'=>$v['elapsedtime']
                        );
                        if (!$id = $this->paomeDb->getOne("select id from r_pm_club_status_list where contest_id={$val['id']} and club_id={$v['club_id']} and `type`={$data['type']}")) {
                            $this->paomeDb->insertRow('r_pm_club_status_list',$data2,0);
                        }else {
                            $setStr = common::arrayToString($data2);
                            $sql = "update r_pm_club_status_list set $setStr where id= $id";
                            $this->paomeDb->update($sql);
                        }
                    }
                }
                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=20
                                              and contest_id={$val['id']}
                                              and status=20");
                foreach ($list as $k=>$v) {
                    $data = $arrResult[$v['club_id']];
                    if (!$data) continue;
                    if ($data['status'] == 15) {
                       continue;
                    } else {
                        $setStr = common::arrayToString($data);
                        $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                        $this->paomeDb->update($sql);
                    }

                }
                break;
            case 31://第三轮决赛结束空档期(22:00-24:00)
                $sql = "update r_pm_club_status set status=20 where contest_id={$val['id']} and `type`=25 and status=15";
                $this->paomeDb->update($sql);
                $sql = "update r_pm_club_status_list set status=20 where contest_id={$val['id']} and `type`=25 and status=15";
                $this->paomeDb->update($sql);
                break;
            case 32://第三轮冠军俱乐部筛选
                $list = $this->paomeDb->getAll("select * from r_pm_club_status
                                              where `type`=25
                                              and contest_id={$val['id']}
                                              and status=20");
                $arrResult = [];//结果数据复用
                foreach ($list as $k=>$v) {
                    $arr = $this->paomeDb->getRow("select status,user_num from r_pm_club where id={$v['club_id']}");//优化
                    if (!$arr) continue;
                    $rankNum = $this->base->load('paome/rank')->getClubRankNumOk($v['club_id'],$val['id'],10);//第三轮预赛
                    $rankNumOld = $this->base->load('paome/rank')->getClubRankNum($v['club_id'],$val['id'],10);//第三轮预赛
                    /*发证书start*/
                    $paomeId = $val['id'];
                    $roundId = 10;
                    $typeWd = 1;//里程
                    $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
                    $rankValue = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
                    $this->base->load('paome/paomeClub')->sendCertificateToClub($val['id'], $v['club_id'], $roundId, $typeWd, (int)$rankNumOld, (double)$rankValue);
                    /*发证书end*/

                    if($arr['user_num'] < $val['min_num'] || $arr['user_num'] > $val['max_num']) {
                        $data = array('type'=>25,'status'=>10);
                    } else if ($rankNum == null || $rankNum>10) { //跑量10进入冠军争霸赛
                        //$data = array('type'=>25,'status'=>10);//淘汰：排名大于10
                        continue;
                    } else {
                        //增加争霸赛资格用户记录
                        $data = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'create_time'=>time(),
                            'type'=>0,
                            'round_type'=>10
                        );
                        $r = $this->paomeDb->insertRow('r_pm_champions_member',$data,0);
                        //增加争霸赛资格redis记录
                        $rankClubKeyMileZbsTicket = 'rankClub-mileageZbs-ticker-'.$val['id'].'-10';//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
                        $this->redis->hSet($rankClubKeyMileZbsTicket,$v['club_id'],1);
                        continue;
                    }
                    $arrResult[$v['club_id']] = $data;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status set $setStr where id= {$v['id']}";
                    $r = $this->paomeDb->update($sql);
//                    if ($r && ($data['status']==10)) {//维度：仅跑量，规则:淘汰俱乐部退出当前轮次排名，加入到淘汰redis列表
//                        $paomeId = $val['id'];
//                        $roundId = 10;
//                        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
//                        $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
//
//                        $failMile = $this->redis->zScore($rankClubKeyMile,$v['club_id']);
//                        $failMileRank = $this->redis->zRevRank($rankClubKeyMile,$v['club_id']);
//                        //写入俱乐部淘汰表
//                        $this->base->load('paome/paomeClub')->addClubOver(
//                            array(
//                                'paomeId'=>$paomeId,
//                                'clubId'=>$v['club_id'],
//                                'roundId2'=>25,//止步于第三轮冠军赛
//                                'historyRank'=>$failMileRank!==false ? $failMileRank+1 : 0,
//                                'mileage'=>(double)$failMile,
//                            )
//                        );
//                        $this->redis->zRem($rankClubKeyMile,$v['club_id']);
//                        $this->redis->zRem($rankClubKeyMileOk,$v['club_id']);
//                    }
                }

                $list = $this->paomeDb->getAll("select id,club_id from r_pm_club_status_list
                                              where `type`=25
                                              and contest_id={$val['id']}
                                              and status=20");
                foreach ($list as $k=>$v) {
                    $data = $arrResult[$v['club_id']];
                    if (!$data) continue;
                    $setStr = common::arrayToString($data);
                    $sql = "update r_pm_club_status_list set $setStr where id= {$v['id']}";
                    $this->paomeDb->update($sql);
                }
                break;
            case 105://冠军争霸赛俱乐部筛选
                error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛俱乐部筛选:start'.PHP_EOL,3,$logPath);
                $arrPaome = $this->getCurrentPaomeInfo('id');
                if(!$arrPaome) return false;
                $list = $this->paomeDb->getAll("select * from r_pm_champions_member");
                if (!$list) {
                    error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛俱乐部筛选-0:资格用户不存在'.PHP_EOL,3,$logPath);
                    return false;
                }
                $roundId  = 100;
                $roundId2 =100;
                $paomeId = $arrPaome['id'];
                $time = time();
                //跑量
                $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
                $rankClubDayKeyMile = 'rankClubDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天（争霸赛：用于查socre非排名）
                $rankClubDayKeyMileNew = 'rankClubDay-mileageNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天（争霸赛：用于查排名非score）
                $rankClubJdKeyMile= 'rankClubJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
                //配速
                $rankClubKeySpeed = 'rankClub-speed-'.$paomeId.'-'.$roundId;
                $rankClubDayKeySpeed = 'rankClubDay-speed-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天(争霸赛：用于查score非排名)
                $rankClubDayKeySpeedNew = 'rankClubDay-speedNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天(争霸赛：用于查排名非score)
                $rankClubJdKeySpeed = 'rankClubJd-speed-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
                //人气
                $rankClubKeySupportNum = 'rankCLub-supportNum-'.$paomeId.'-'.$roundId;
                $rankClubDayKeySupportNum = 'rankClubDay-supportNum-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天(争霸赛：用于查score非排名)
                //$rankClubDayKeySupportNumNew = 'rankClubDay-supportNumNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);////天(争霸赛：用于查排名非score)
                $rankClubJdKeySupportNum = 'rankClubJd-supportNum-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
                foreach ($list as $k=>$v) {
                    $arrClubRounds = $this->paomeDb->getOne("select id from r_pm_club_rounds
                                                    where contest_id={$val['id']}
                                                    and club_id={$v['club_id']}
                                                    and type=100");
                    if (!$arrClubRounds) {
                        error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛-$arrClubRounds-战队ID:'.var_export($v['club_id'], true).PHP_EOL,3,$logPath);
                        $data = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'create_time'=>time(),
                            'type'=>100
                        );
                        $r2 = $this->paomeDb->insertRow('r_pm_club_rounds',$data,0);
                        //初始化战队排行数据
                        $this->redis->zAdd($rankClubKeyMile, 0 ,$v['club_id']);
                        $this->redis->zAdd($rankClubDayKeyMile, 0, $v['club_id']);
                        $this->redis->zAdd($rankClubDayKeyMileNew, 0, $v['club_id']);
                        $this->redis->zAdd($rankClubJdKeyMile, 0, $v['club_id']);

                        /*默认全部放出，配速值由快到慢，避免新产生的配速小于默认0，给默认值设置成：10000*/
                        $this->redis->zAdd($rankClubKeySpeed, self::DEFAULT_SPEED_RANK_VALUE, $v['club_id']);
                        $this->redis->zAdd($rankClubDayKeySpeed, self::DEFAULT_SPEED_RANK_VALUE, $v['club_id']);
                        $this->redis->zAdd($rankClubDayKeySpeedNew, self::DEFAULT_SPEED_RANK_VALUE, $v['club_id']);
                        $this->redis->zAdd($rankClubJdKeySpeed, self::DEFAULT_SPEED_RANK_VALUE, $v['club_id']);

                        $this->redis->zAdd($rankClubKeySupportNum, 0, $v['club_id']);
                        $this->redis->zAdd($rankClubDayKeySupportNum, 0, $v['club_id']);
                        //$this->redis->zAdd($rankClubDayKeySupportNumNew, 0, $v['club_id']);
                        $this->redis->zAdd($rankClubJdKeySupportNum, 0, $v['club_id']);
                        //全员跑量榜无法全部初始化,改成有跑步记录自动生成
                        //初始化表数据
                        /*积分表 start*/
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId,
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_score set {$insertStr} on duplicate key update {$insertStr}");

                        $data = array(
                                'contest_id'=>$paomeId,
                                'club_id'=>$v['club_id'],
                                'type'=>$roundId,
                                'date'=>date('Ymd',$time),
                                'create_time'=>$time
                            );
                            $insertStr = common::arrayToString($data);
                            $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_score_day set {$insertStr} on duplicate key update {$insertStr}");
                        /*积分表 end*/

                        /*排名轮次表 start*/
                        //里程
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId,
                            'type_wd'=>1,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank set {$insertStr} on duplicate key update {$insertStr}");

                        //配速
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId,
                            'type_wd'=>5,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank set {$insertStr} on duplicate key update {$insertStr}");

                        //人气
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId,
                            'type_wd'=>10,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank set {$insertStr} on duplicate key update {$insertStr}");
                        /*排名轮次表 end*/

                        /*排名轮次天表 start*/
                        //里程
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId,
                            'type_wd'=>1,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'date'=>date('Ymd',$time),
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_day set {$insertStr} on duplicate key update {$insertStr}");

                        //配速
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId,
                            'type_wd'=>5,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'date'=>date('Ymd',$time),
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_day set {$insertStr} on duplicate key update {$insertStr}");

                        //人气
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId,
                            'type_wd'=>10,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'date'=>date('Ymd',$time),
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);//需要测试
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_day set {$insertStr} on duplicate key update {$insertStr}");
                        /*排名轮次天表 end*/

                        /*排名轮次阶段表 start*/
                        //里程
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId2,
                            'type_wd'=>1,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_jd set {$insertStr} on duplicate key update {$insertStr}");

                        //配速
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId2,
                            'type_wd'=>5,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);//需要测试
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_jd set {$insertStr} on duplicate key update {$insertStr}");

                        //人气
                        $data = array(
                            'contest_id'=>$paomeId,
                            'club_id'=>$v['club_id'],
                            'type'=>$roundId2,
                            'type_wd'=>10,
                            'object_id'=>$v['club_id'],
                            'status'=>0,
                            'create_time'=>$time
                        );
                        $insertStr = common::arrayToString($data);//需要测试
                        $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_jd set {$insertStr} on duplicate key update {$insertStr}");
                        /*排名轮次阶段表 end*/
                    }
                    $arrClubRoundStatus = $this->paomeDb->getRow("select id,`type` from r_pm_club_status
                                              where  contest_id={$val['id']}
                                              and  club_id = {$v['club_id']}");
                    if(!$arrClubRoundStatus) {
                        error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛-r_pm_club_status没有记录-战队ID:'.var_export($v['club_id'], true).PHP_EOL,3,$logPath);
                        $data = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'type'=>100,
                            'status'=>15,
                            'create_time'=>time()
                        );
                        $r = $this->paomeDb->insertRow('r_pm_club_status',$data,0);
                        if (!$r) {
                            error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛-r_pm_club_status没有记录-插入异常-战队ID:'.var_export($v['club_id'], true).PHP_EOL,3,$logPath);
                            continue;
                        }//异常
                    } else {
                        error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛-r_pm_club_status有记录A-战队ID:'.var_export($v['club_id'], true).PHP_EOL,3,$logPath);
                        if ($arrClubRoundStatus['type'] != 100) {
                            error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛-r_pm_club_status有记录不是争霸赛-战队ID:'.var_export($v['club_id'], true).' -阶段ID:'.var_export($arrClubRoundStatus['type'], true).PHP_EOL,3,$logPath);
                            $data = array(
                                'type'=>100,
                                'status'=>15,
                                'mileage'=>0,
                                'speed'=>0,
                                'support_num'=>0,
                                'energy_num'=>0,
                                'agree_num'=>0,
                                'support_user_num'=>0,
                                'elapsedtime'=>0,
                                'mileage_three'=>0
                            );
                            $setStr = common::arrayToString($data);
                            $sql = "update r_pm_club_status set $setStr where id= {$arrClubRoundStatus['id']}";
                            $this->paomeDb->update($sql);
                        }
                    }

                    $clubRoundStatusListId = $this->paomeDb->getOne("select id from r_pm_club_status_list
                                              where `type`=100
                                              and contest_id={$val['id']}
                                              and  club_id = {$v['club_id']}");
                    if(!$clubRoundStatusListId) {
                        error_log('时间'.date('Y-m-d H:i:s').' 105冠军争霸赛-r_pm_club_status_list无记录-战队ID:'.var_export($v['club_id'], true).PHP_EOL,3,$logPath);
                        $data = array(
                            'contest_id'=>$val['id'],
                            'club_id'=>$v['club_id'],
                            'type'=>100,
                            'status'=>15,
                            'create_time'=>time()
                        );
                        $r = $this->paomeDb->insertRow('r_pm_club_status_list',$data,0);
                    }
                }
                break;
            case 106://冠军争霸赛结束空档期(22:00-24:00)
                $sql = "update r_pm_club_status set status=20 where contest_id={$val['id']} and `type`=100 and status=15";
                $this->paomeDb->update($sql);
                $sql = "update r_pm_club_status_list set status=20 where contest_id={$val['id']} and `type`=100 and status=15";
                $this->paomeDb->update($sql);
                //通知发证书
                break;
            case 120://冠军筛选
                $list = $this->paomeDb->getAll("select * from r_pm_champions_member");
                if (!$list) break;
                $paomeId = $val['id'];
                $roundId = 100;
                foreach ($list as $k=>$v) {
                    /*积分必须等同步脚本先跑完*/
                    $typeWd = 20;//积分
                    $arrScore = $this->paomeDb->getRow("select score_rank_num,total_score,mileage_score,speed_score,support_score from r_pm_score
                                             where contest_id=$paomeId
                                             and club_id= {$v['club_id']}
                                             and `type`=$roundId");

                    $rankNum = $arrScore && isset($arrScore['score_rank_num']) ? $arrScore['score_rank_num'] : 0;//第三轮预赛
                    $rankValue =  $arrScore && isset($arrScore['total_score']) ? $arrScore['total_score'] : 0;
                    $mileageScore = $arrScore && isset($arrScore['mileage_score']) ? $arrScore['mileage_score'] : 0;
                    $speedScore = $arrScore && isset($arrScore['speed_score']) ? $arrScore['speed_score'] : 0;
                    $supportScore = $arrScore && isset($arrScore['support_score']) ? $arrScore['support_score'] : 0;
                    /*发证书start*/
                    $this->base->load('paome/paomeClub')->sendCertificateToClub($val['id'], $v['club_id'], $roundId, $typeWd, $rankNum, $rankValue, $mileageScore, $speedScore, $supportScore);
                    /*发证书end*/
                }
                break;
            default:
                break;
        }
    }

    /**
     * ③状态变更完成,初始化今天排名（同步redis中昨日的排名到今天数据中）
     * 影响跑量排名成员数量
     * 配速，人气，全员跑量榜直接copy排名值，这3个维度 展现的时候，淘汰的俱乐部或退出的成员也要展示，不显示排名升降说明文字
     */
    public function initTodayRankFromYesterdayData() {
        $logPath = $this->base->config['log-dir'].'api/addUserRunData-'.date('Y-m-d').'.log';
        error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名 START:'.PHP_EOL,3,$logPath);
        $sql = "select * from r_pm_contest where flag=1 and status>=5 and status<120";
        $info = $this->paomeDb->getRow($sql);
        if (!$info) return false;
        //查询昨天是否是比赛状态
        //当前时间戳
        $nowTime = time();
        $yesterdayTime = time()-86400;
        //查询昨天是否有比赛,在比赛结束，第二天同步第一天排行数据
        $arrCanLoadStatus = common::$arrCanLoadStatus;//5,6,10,11,15,16,20,21,25,26,30,31,105,106
        $info = $this->paomeDb->getRow("select * from r_pm_rounds
                                            where $yesterdayTime >=start_time
                                             and $yesterdayTime<=end_time
                                              and type in(".implode(',',$arrCanLoadStatus).")
                                              and status=0");
        if (!$info) {
            error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--查询昨天是否有比赛 终止:'.PHP_EOL,3,$logPath);
            return false;
        }
        $paomeId = $info['contest_id'];
        $paomeStatus = $info['type'];
        list($roundId, $roundId2) = $this->getCurrentRoundInfo($paomeStatus);
        error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--查询轮次ID-阶段ID'.var_export(array($roundId, $roundId2), true).PHP_EOL,3,$logPath);
        if (!$roundId || !$roundId2) return false;
        /*昨天数据*/
            //跑量
            $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//轮次
            $rankClubDayKeyMile = 'rankClubDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
            $rankClubJdKeyMile= 'rankClubJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
            //配速
            $rankClubDayKeySpeed = 'rankClubDay-speed-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
            $rankClubJdKeySpeed = 'rankClubJd-speed-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
            //人气
            $rankClubDayKeySupportNum = 'rankClubDay-supportNum-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
            $rankClubJdKeySupportNum = 'rankClubJd-supportNum-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
            //全员跑量
            $rankUserDayKeyMile = 'rankUserDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
            $rankUserJdKeyMile = 'rankUserJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
        /*今天同步数据*/
        $info2 = $this->paomeDb->getRow("select * from r_pm_rounds
                                            where $nowTime >=start_time
                                              and $nowTime<=end_time
                                              and type in(".implode(',',$arrCanLoadStatus).")
                                              and status=0");
        if (!$info2) {
            error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--查询今天是否有比赛--> 终止:'.PHP_EOL,3,$logPath);
            return false;
        }
        $paomeId = $info2['contest_id'];
        $paomeStatus2 = $info2['type'];
        list($roundId11, $roundId22) = $this->getCurrentRoundInfo($paomeStatus2);
        if (!$roundId11 || !$roundId22) return false;
            //跑量
            $rankClubDayKeyMile2 = 'rankClubDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d');//天
            $rankClubJdKeyMile2= 'rankClubJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId22;//阶段
            $rankClubDayKeyMileNew = 'rankClubDay-mileageNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d');//天（争霸赛：用于查排名非score）

        //配速
            $rankClubDayKeySpeed2 = 'rankClubDay-speed-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d');//天
            $rankClubJdKeySpeed2 = 'rankClubJd-speed-'.$paomeId.'-'.$roundId.'-'.$roundId22;//阶段
            $rankClubDayKeySpeedNew = 'rankClubDay-speedNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d');//天(争霸赛：用于查排名非score)

        //人气
            $rankClubDayKeySupportNum2 = 'rankClubDay-supportNum-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d');//天
            $rankClubJdKeySupportNum2 = 'rankClubJd-supportNum-'.$paomeId.'-'.$roundId.'-'.$roundId22;//阶段
            //全员跑量
            $rankUserDayKeyMile2 = 'rankUserDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d');//天
            $rankUserJdKeyMile2 = 'rankUserJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId22;//阶段

        /*跑量同步开始(正常参与的俱乐部排行)*/
            //天
            $list = $this->redis->zRevRange($rankClubDayKeyMile, 0, -1, true);
            if ($list) {
                error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 里程开始:'.PHP_EOL,3,$logPath);
                foreach ($list as $clubId=>$mile) { //战队在正常比赛才同步，预赛到冠军赛的临界点，取得冠军资格用户（redis正常用户）里程数据才同步
                    if($roundId == 100) {//争霸赛新规 跑量每天清空,完成初始化放在缓存,在列表都展示
                        $tempRankValue = $this->base->load('paome/rank')->getObjectDayRankValue($clubId, $paomeId, $roundId, date('Ymd'), 1);
                        error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 里程CONTINUE:当天是否已经产生记录 tempRankValue'.var_export($tempRankValue, true).PHP_EOL,3,$logPath);
                        if(!$tempRankValue) {
                           $this->redis->zAdd($rankClubDayKeyMile2, 0, $clubId);//排行数据清空
                           $this->redis->zAdd($rankClubDayKeyMileNew, 0, $clubId);//排行值清空
                       }
                    } else {
                        if ($this->redis->zRank($rankClubKeyMile, $clubId)!== false &&
                            $this->redis->zRank($rankClubDayKeyMile2, $clubId) === false) {
                            $this->redis->zAdd($rankClubDayKeyMile2, $mile, $clubId);
                        }
                    }
                }
                error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 里程结束:'.PHP_EOL,3,$logPath);
            }
           //阶段
            $list = $this->redis->zRevRange($rankClubJdKeyMile, 0, -1, true);//跑步上传触发创建或晋级冠军成功，定时器自动创建
            if ($list) {
                foreach ($list as $clubId=>$mile) {
                    if ($this->redis->zRank($rankClubKeyMile, $clubId)!== false &&
                        $this->redis->zRank($rankClubJdKeyMile2, $clubId) === false) {
                        $this->redis->zAdd($rankClubJdKeyMile2, $mile, $clubId);
                    }
                }
            }
        /*跑量同步结束*/

        /*配速同步开始（每轮所有状态的俱乐部）*/
        //天
        $list = $this->redis->zRange($rankClubDayKeySpeed, 0, -1, true);
        if ($list) {
            error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 配速开始:'.PHP_EOL,3,$logPath);
            foreach ($list as $clubId=>$mile) {
                if($roundId == 100) {//争霸赛新规 配速每天清空,完成初始化放在缓存,在列表都展示
                    $tempRankValue = $this->base->load('paome/rank')->getObjectDayRankValue($clubId, $paomeId, $roundId, date('Ymd'), 5, 0);
                    error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 配速CONTINUE:当天是否已经产生记录 tempRankValue'.var_export($tempRankValue, true).PHP_EOL,3,$logPath);
                    if (!$tempRankValue) {
                        $this->redis->zAdd($rankClubDayKeySpeed2, self::DEFAULT_SPEED_RANK_VALUE, $clubId);//日排行数据清空
                        $this->redis->zAdd($rankClubDayKeySpeedNew, self::DEFAULT_SPEED_RANK_VALUE, $clubId);//日排行值清空
                    }
                } else {
                    if ($this->redis->zRank($rankClubDayKeySpeed2, $clubId) === false) {
                        $this->redis->zAdd($rankClubDayKeySpeed2, $mile, $clubId);
                    }
                }
            }
            error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 配速结束:'.PHP_EOL,3,$logPath);
        }
        //阶段
        $list = $this->redis->zRange($rankClubJdKeySpeed, 0, -1, true);//跑步上传触发创建或定时器自动创建
        if ($list) {
            foreach ($list as $clubId=>$mile) {
                if ($this->redis->zRank($rankClubJdKeySpeed2, $clubId) === false) {
                    $this->redis->zAdd($rankClubJdKeySpeed2, $mile, $clubId);
                }
            }
        }
        /*配速同步结束*/

        /*人气同步开始*/
        $list = $this->redis->zRevRange($rankClubDayKeySupportNum, 0, -1, true);
        if ($list) {
            error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 人气开始:'.PHP_EOL,3,$logPath);
            foreach ($list as $clubId=>$mile) {
                if ($this->redis->zRank($rankClubDayKeySupportNum2, $clubId) === false) {
                    error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 人气Continue-1:'.PHP_EOL,3,$logPath);
                    $this->redis->zAdd($rankClubDayKeySupportNum2, $mile, $clubId);
                } else { //如果0点点赞了，（之前）
                    error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 人气Continue-2:'.PHP_EOL,3,$logPath);
                    $this->redis->zIncrBy($rankClubDayKeySupportNum2, $mile, $clubId);
                }
            }
            error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名--战队天同步--> 人气结束:'.PHP_EOL,3,$logPath);
        }
        //阶段
        $list = $this->redis->zRevRange($rankClubJdKeySupportNum, 0, -1, true);//跑步上传触发创建或定时器自动创建
        if ($list) {
            foreach ($list as $clubId=>$mile) {
                if ($this->redis->zRank($rankClubJdKeySupportNum2, $clubId) === false) {
                    $this->redis->zAdd($rankClubJdKeySupportNum2, $mile, $clubId);
                } else {
                    $this->redis->zIncrBy($rankClubJdKeySupportNum2, $mile, $clubId);
                }
            }
        }
        /*人气同步结束*/

        /*全员跑量榜同步开始*/
        $list = $this->redis->zRevRange($rankUserDayKeyMile, 0, -1, true);
        if ($list) {
            foreach ($list as $uid=>$mile) {
                if ($this->redis->zRank($rankUserDayKeyMile2, $uid) === false) {
                    $this->redis->zAdd($rankUserDayKeyMile2, $mile, $uid);
                }
            }
        }
        //阶段
        $list = $this->redis->zRevRange($rankUserJdKeyMile, 0, -1, true);//跑步上传触发创建或定时器自动创建
        if ($list) {
            foreach ($list as $uid=>$mile) {
                if ($this->redis->zRank($rankUserJdKeyMile2, $uid) === false) {
                    $this->redis->zAdd($rankUserJdKeyMile2, $mile, $uid);
                }
            }
        }
        /*全员跑量榜同步结束*/
        error_log('时间'.date('Y-m-d H:i:s').' 状态变更完成,初始化今天排名 END:'.PHP_EOL,3,$logPath);
    }

    /**
     * 每晚凌晨3点半跑一次
     * 更新redis维度排名
     */
    public function syncRedisRankNum() {
        $logPath = $this->base->config['log-dir'].'api/addUserRunData-'.date('Y-m-d').'.log';
        error_log('时间'.date('Y-m-d H:i:s').' start每晚凌晨3点半跑一次记录-->'.PHP_EOL,3,$logPath);
        //当前时间戳
        $nowTime = time();
        $yesterdayTime = time()-86400;
        //查询昨天是否有比赛,在比赛结束，第二天统计排行
        $arrCanLoadStatus = common::$arrCanLoadStatus;//5,6,10,11,15,16,20,21,25,26,30,31,105,106
        $info = $this->paomeDb->getRow("select * from r_pm_rounds
                                            where $yesterdayTime >=start_time
                                             and $yesterdayTime<=end_time
                                             and type in(".implode(',',$arrCanLoadStatus).")
                                             and status=0");
        if (!$info) {
            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->info:sql'.
                "select * from r_pm_rounds
                    where $yesterdayTime >=start_timeck
                    and $yesterdayTime<=end_time
                    and type in(".implode(',',$arrCanLoadStatus).")
                    and status=0"
                .PHP_EOL,3,$logPath);
            return false;
        }
        $paomeId = $info['contest_id'];
        $paomeStatus = $info['type'];
        list($roundId, $roundId2) = $this->getCurrentRoundInfo($paomeStatus);
        if (!$roundId || !$roundId2){
            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->轮次：'.var_export($roundId.'-'.$roundId2,true).PHP_EOL,3,$logPath);
            return false;
        }
        //查询redis数据
        //跑量  总排行数据赛季时期内每天晚上同步一次
        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;
        $rankClubDayKeyMile = 'rankClubDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
        $rankClubJdKeyMile= 'rankClubJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
        //配速
        $rankClubKeySpeed = 'rankClub-speed-'.$paomeId.'-'.$roundId;
        $rankClubDayKeySpeed = 'rankClubDay-speed-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
        $rankClubJdKeySpeed = 'rankClubJd-speed-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
        //人气
        $rankClubKeySupportNum = 'rankCLub-supportNum-'.$paomeId.'-'.$roundId;
        $rankClubDayKeySupportNum = 'rankClubDay-supportNum-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
        $rankClubJdKeySupportNum = 'rankClubJd-supportNum-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
        //全员跑量
        $rankUserKeyMile = 'rankUser-mileage-'.$paomeId.'-'.$roundId;
        $rankUserDayKeyMile = 'rankUserDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$yesterdayTime);//天
        $rankUserJdKeyMile = 'rankUserJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段

        //先同步r_pm_rank 总表
        $arrMileRank = $this->redis->zRevRange($rankClubKeyMile,0,-1);
        if ($arrMileRank) {
            foreach($arrMileRank as $key=>$val) {
                $sql = "select id from r_pm_rank where contest_id=$paomeId and club_id=$val and object_id=$val and `type`=$roundId and type_wd=1";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank set rank_num=$key+1 where id={$info['id']}");
                } else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $val;
                    $data['type'] = $roundId;
                    $data['type_wd'] = 1;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankClubKeyMile,$val) !==false) ? $this->redis->zScore($rankClubKeyMile,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        $arrSpeedRank = $this->redis->zRange($rankClubKeySpeed,0,-1);
        if ($arrSpeedRank) {
            foreach($arrSpeedRank as $key=>$val) {
                $sql = "select id from r_pm_rank where contest_id=$paomeId and club_id=$val and object_id=$val and `type`=$roundId and type_wd=5";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank set rank_num=$key+1 where id={$info['id']}");
                } else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $val;
                    $data['type'] = $roundId;
                    $data['type_wd'] = 5;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankClubKeySpeed,$val)!==false) ? $this->redis->zScore($rankClubKeySpeed,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        $arrSupportNumRank = $this->redis->zRevRange($rankClubKeySupportNum,0,-1);
        if ($arrSupportNumRank) {
            foreach($arrSupportNumRank as $key=>$val) {
                $sql = "select id from r_pm_rank where contest_id=$paomeId and club_id=$val and object_id=$val and `type`=$roundId and type_wd=10";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank set rank_num=$key+1 where id={$info['id']}");
                }  else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $val;
                    $data['type'] = $roundId;
                    $data['type_wd'] = 10;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankClubKeySupportNum,$val)!==false) ? $this->redis->zScore($rankClubKeySupportNum,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        $arrUserRank = $this->redis->zRevRange($rankUserKeyMile,0,-1);
        if ($arrUserRank) {
            foreach($arrUserRank as $key=>$val) {
                //用户所在俱乐部
                $clubId = $this->paomeDb->getOne("select club_id from r_pm_club_users where uid=$val and status=0");
                if (!$clubId) continue;
                $sql = "select id from r_pm_rank where contest_id=$paomeId and club_id=$clubId and object_id=$val and `type`=$roundId and type_wd=15";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank set rank_num=$key+1 where id={$info['id']}");
                }  else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $clubId;
                    $data['type'] = $roundId;
                    $data['type_wd'] = 15;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankUserKeyMile,$val)!==false) ? $this->redis->zScore($rankUserKeyMile,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        //然后同步r_pm_rank_jd 阶段表
        $arrMileJdRank = $this->redis->zRevRange($rankClubJdKeyMile,0,-1);
        if ($arrMileJdRank) {
            foreach($arrMileJdRank as $key=>$val) {
                $sql = "select id from r_pm_rank_jd where contest_id=$paomeId and club_id=$val and object_id=$val and `type`=$roundId2 and type_wd=1";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank_jd set rank_num=$key+1 where id={$info['id']}");
                } else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $val;
                    $data['type'] = $roundId2;
                    $data['type_wd'] = 1;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankClubJdKeyMile,$val)!==false) ? $this->redis->zScore($rankClubJdKeyMile,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank_jd',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        $arrSpeedJdRank = $this->redis->zRange($rankClubJdKeySpeed,0,-1);
        if ($arrSpeedJdRank) {
            foreach($arrSpeedJdRank as $key=>$val) {
                $sql = "select id from r_pm_rank_jd where contest_id=$paomeId and club_id=$val and object_id=$val and `type`=$roundId2 and type_wd=5";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank_jd set rank_num=$key+1 where id={$info['id']}");
                } else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $val;
                    $data['type'] = $roundId2;
                    $data['type_wd'] = 5;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankClubJdKeySpeed,$val)!==false) ? $this->redis->zScore($rankClubJdKeySpeed,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank_jd',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        $arrSupportNumJdRank = $this->redis->zRevRange($rankClubJdKeySupportNum,0,-1);
        if ($arrSupportNumJdRank) {
            foreach($arrSupportNumJdRank as $key=>$val) {
                $sql = "select id from r_pm_rank_jd where contest_id=$paomeId and club_id=$val and object_id=$val and `type`=$roundId2 and type_wd=10";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank_jd set rank_num=$key+1 where id={$info['id']}");
                } else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $val;
                    $data['type'] = $roundId2;
                    $data['type_wd'] = 10;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankClubJdKeySupportNum,$val)!==false) ? $this->redis->zScore($rankClubJdKeySupportNum,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank_jd',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        $arrUserJdRank = $this->redis->zRevRange($rankUserJdKeyMile,0,-1);
        if ($arrUserJdRank) {
            foreach($arrUserJdRank as $key=>$val) {
                //用户所在俱乐部
                $clubId = $this->paomeDb->getOne("select club_id from r_pm_club_users where uid=$val and status=0");
                if (!$clubId) continue;
                $sql = "select id from r_pm_rank_jd where contest_id=$paomeId and club_id=$clubId and object_id=$val and `type`=$roundId2 and type_wd=15";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank_jd set rank_num=$key+1 where id={$info['id']}");
                } else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $clubId;
                    $data['type'] = $roundId2;
                    $data['type_wd'] = 15;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankUserJdKeyMile,$val)!==false) ? $this->redis->zScore($rankUserJdKeyMile,$val) :0;
                    $data['create_time'] = time();
                    $this->paomeDb->insertRow('r_pm_rank_jd',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        //最后同步r_pm_rank_day 每天排行数据
        $arrMileDayRank = $this->redis->zRevRange($rankClubDayKeyMile,0,-1);
        $formartYesterday = date('Ymd',$yesterdayTime);
        error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->轮次：最后同步r_pm_rank_day 每天排行数据:跑量'.var_export($arrMileDayRank, true).PHP_EOL,3,$logPath);
        if ($arrMileDayRank) {
            foreach($arrMileDayRank as $key=>$val) {
                $sql = "select id from r_pm_rank_day where contest_id=$paomeId and club_id=$val and object_id=$val and `date`='$formartYesterday' and `type`=$roundId and type_wd=1";
                $info = $this->paomeDb->getRow($sql);
                $rankNum = $key+1;
                if ($roundId == 100) {//争霸赛新规,日没有记录的在缓存中排名不算，展示为--
                    $rankNum = $this->base->load('paome/rank')->getObjectDayRank($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 1);//日排名
                }
                if($rankNum !== null) {
                    $data =[];
                    $score = 0;
                    if($roundId == 100) {
                        $score = (int)$this->base->load('paome/rank')->getClubRankScore($rankNum, 1);
                    }
                    if ($info) {
                        $this->paomeDb->update("update r_pm_rank_day set rank_num=$rankNum,score=$score where id={$info['id']}");
                    } else {
                        $data['contest_id'] = $paomeId;
                        $data['club_id'] = $val;
                        $data['type'] = $roundId;
                        $data['type_wd'] = 1;
                        $data['object_id'] = $val;
                        $data['status'] = 0;
                        $data['rank_num'] = $rankNum;
                        $data['rank_value'] = ($this->redis->zScore($rankClubDayKeyMile,$val)!==false) ? $this->redis->zScore($rankClubDayKeyMile,$val) :0;
                        $data['create_time'] = time();
                        $data['date'] = $formartYesterday;
                        $data['score'] = $score;
                        $this->paomeDb->insertRow('r_pm_rank_day',$data,mysqldb::INSERT_COMMON);
                        error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->轮次：最后同步r_pm_rank_day 每天排行数据:跑量3:data:'.var_export($data, true).PHP_EOL,3,$logPath);
                    }
                }
                /*同步积分数据start*/
                if($roundId == 100) {
                    $sql = "select id,total_score from r_pm_score_day where contest_id=$paomeId and club_id=$val and `type`=$roundId  and `date`='$formartYesterday'";
                    $arr = $this->paomeDb->getRow("$sql");
                    error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->同步积分数据 开始 战队ID:'.$val.' sql:'.$sql.' result:'.var_export($arr, true).PHP_EOL,3,$logPath);

                    if(!$arr || (($arr && $arr['total_score'] == 0)) ) {
                        $mileageScore = 0;
                        $speedScore = 0;
                        $supportScore = 0;
                        //天里程排名
                        $rankNum = $this->base->load('paome/rank')->getObjectDayRank($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 1);

                        if ($rankNum !== null) {
                            $mileageScore = $this->base->load('paome/rank')->getClubRankScore($rankNum, 1);
                        }
                        //天配速排名
                        $rankNum = $this->base->load('paome/rank')->getObjectDayRank($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 5);
                        if ($rankNum !== null) {
                            $speedScore = $this->base->load('paome/rank')->getClubRankScore($rankNum, 5);
                        }
                        //天人气排名
                        $rankNum = $this->base->load('paome/rank')->getObjectDayRank($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 10);
                        if ($rankNum !== null) {
                            $supportScore = $this->base->load('paome/rank')->getClubRankScore($rankNum, 10);
                        }
                        $totalScore = $mileageScore+$speedScore+$supportScore;//总积分
                        //天里程|配速|人气值
                        $rankValue1 = $this->base->load('paome/rank')->getObjectDayRankValue($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 1);
                        $rankValue2 = $this->base->load('paome/rank')->getObjectDayRankValue($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 5, 0);
                        $rankValue3 = $this->base->load('paome/rank')->getObjectDayRankValue($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 10);

                        if(!$arr){
                            $insertStr = "contest_id=$paomeId,
                                        club_id       = $val,
                                        type          = $roundId,
                                        create_time   = $yesterdayTime,
                                        total_score   = $totalScore,
                                        mileage_score = $mileageScore,
                                        speed_score   = $speedScore,
                                        support_score = $supportScore,
                                        mileage       = $rankValue1,
                                        speed         = $rankValue2,
                                        support_num   = $rankValue3";
                            $insertStr .= ",date=".date('Ymd',$yesterdayTime);
                            $re = $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_score_day set {$insertStr} on duplicate key update {$insertStr}");
                            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->同步积分数据 continue-1-new 战队ID:'.$val.' 参数:'.var_export($insertStr, true).' result'.var_export($re, true).PHP_EOL,3,$logPath);
                            $insertStr = "contest_id=$paomeId,
                                        club_id       = $val,
                                        type          = $roundId,
                                        create_time   = $yesterdayTime,
                                        total_score   = total_score+$totalScore,
                                        mileage_score = mileage_score+$mileageScore,
                                        speed_score   = speed_score+$speedScore,
                                        support_score = support_score+$supportScore,
                                        mileage       = $rankValue1,
                                        speed         = $rankValue2,
                                        support_num   = $rankValue3";
                            $re2 = $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_score set {$insertStr} on duplicate key update {$insertStr}");
                            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->同步积分数据 continue-2-new 战队ID:'.$val.' 参数:'.var_export($insertStr, true).' result'.var_export($re2, true).PHP_EOL,3,$logPath);
                        }else if (($arr['total_score'] == 0)) {
                            $insertStr = "contest_id=$paomeId,
                                        club_id       = $val,
                                        type          = $roundId,
                                        create_time   = $yesterdayTime,
                                        total_score   = total_score+$totalScore,
                                        mileage_score = mileage_score+$mileageScore,
                                        speed_score   = speed_score+$speedScore,
                                        support_score = support_score+$supportScore,
                                        mileage       = $rankValue1,
                                        speed         = $rankValue2,
                                        support_num   = $rankValue3";
                            $re = $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_score set {$insertStr} on duplicate key update {$insertStr}");
                            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->同步积分数据 continue-1 战队ID:'.$val.' 参数:'.var_export($insertStr, true).' result'.var_export($re, true).PHP_EOL,3,$logPath);
                            $insertStr .= ",date=".date('Ymd',$yesterdayTime);
                            $re2 = $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_score_day set {$insertStr} on duplicate key update {$insertStr}");
                            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->同步积分数据 continue-2 战队ID:'.$val.' 参数:'.var_export($insertStr, true).' result'.var_export($re2, true).PHP_EOL,3,$logPath);
                        }
                    }
                    /*同步积分数据end*/
                    }

            }
        }
        $arrSpeedDayRank = $this->redis->zRange($rankClubDayKeySpeed,0,-1);
        if ($arrSpeedDayRank) {
            foreach($arrSpeedDayRank as $key=>$val) {
                $sql = "select id from r_pm_rank_day where contest_id=$paomeId and club_id=$val and object_id=$val and `date`='$formartYesterday' and `type`=$roundId and type_wd=5";
                $info = $this->paomeDb->getRow($sql);
                $rankNum = $key+1;
                if ($roundId == 100) {//争霸赛新规,日没有记录的在缓存中排名不算，展示为--
                    $rankNum = $this->base->load('paome/rank')->getObjectDayRank($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 5);//日排名
                }
                if($rankNum !== null) {
                    $data =[];
                    $score= 0;
                    if ($roundId == 100) {
                        $score = $this->base->load('paome/rank')->getClubRankScore($rankNum, 5);
                    }
                    if ($info) {
                        $this->paomeDb->update("update r_pm_rank_day set rank_num=$rankNum,score=$score where id={$info['id']}");
                    } else {
                        $data['contest_id'] = $paomeId;
                        $data['club_id'] = $val;
                        $data['type'] = $roundId;
                        $data['type_wd'] = 5;
                        $data['object_id'] = $val;
                        $data['status'] = 0;
                        $data['rank_num'] = $rankNum;
                        $data['rank_value'] = ($this->redis->zScore($rankClubDayKeySpeed,$val)!==false) ? $this->redis->zScore($rankClubDayKeySpeed,$val) :0;
                        $data['create_time'] = time();
                        $data['date'] = $formartYesterday;
                        $data['score'] = $score;
                        $this->paomeDb->insertRow('r_pm_rank_day',$data,mysqldb::INSERT_COMMON);
                    }
                }

            }
        }
        $arrSupportNumDayRank = $this->redis->zRevRange($rankClubDayKeySupportNum,0,-1);
        if ($arrSupportNumDayRank) {
            foreach($arrSupportNumDayRank as $key=>$val) {
                $sql = "select id from r_pm_rank_day where contest_id=$paomeId and club_id=$val and object_id=$val and `date`='$formartYesterday' and `type`=$roundId2 and type_wd=10";
                $info = $this->paomeDb->getRow($sql);
                $rankNum = $key+1;
                if ($roundId == 100) {//争霸赛新规,日没有记录的在缓存中排名不算，展示为--
                    $rankNum = $this->base->load('paome/rank')->getObjectDayRank($val, $paomeId, $roundId, date('Ymd',$yesterdayTime), 10);//日排名
                }
                if($rankNum !== null)  {
                    $data =[];
                    $score = 0;
                    if ($roundId == 100) {
                        $score = $this->base->load('paome/rank')->getClubRankScore($rankNum, 10);
                    }
                    if ($info) {
                        $this->paomeDb->update("update r_pm_rank_day set rank_num=$rankNum,score=$score where id={$info['id']}");
                    } else {
                        $data['contest_id'] = $paomeId;
                        $data['club_id'] = $val;
                        $data['type'] = $roundId;
                        $data['type_wd'] = 10;
                        $data['object_id'] = $val;
                        $data['status'] = 0;
                        $data['rank_num'] = $rankNum;
                        $data['rank_value'] = ($this->redis->zScore($rankClubDayKeySupportNum,$val)!==false) ? $this->redis->zScore($rankClubDayKeySupportNum,$val) :0;
                        $data['create_time'] = time();
                        $data['date'] = $formartYesterday;
                        $data['score'] = $score;
                        $this->paomeDb->insertRow('r_pm_rank_day',$data,mysqldb::INSERT_COMMON);
                    }
                }

            }
        }
        $arrUserDayRank = $this->redis->zRevRange($rankUserDayKeyMile,0,-1);
        if ($arrUserDayRank) {
            foreach($arrUserDayRank as $key=>$val) {
                //用户所在俱乐部
                $clubId = $this->paomeDb->getOne("select club_id from r_pm_club_users where uid=$val and status=0");
                if (!$clubId) continue;
                $sql = "select id from r_pm_rank_day where contest_id=$paomeId and club_id=$clubId and object_id=$val and `date`='$formartYesterday' and `type`=$roundId and type_wd=15";
                $info = $this->paomeDb->getRow($sql);
                if ($info) {
                    $this->paomeDb->update("update r_pm_rank_day set rank_num=$key+1 where id={$info['id']}");
                } else {
                    $data =[];
                    $data['contest_id'] = $paomeId;
                    $data['club_id'] = $clubId;
                    $data['type'] = $roundId;
                    $data['type_wd'] = 15;
                    $data['object_id'] = $val;
                    $data['status'] = 0;
                    $data['rank_num'] = $key+1;
                    $data['rank_value'] = ($this->redis->zScore($rankUserDayKeyMile,$val)!==false) ? $this->redis->zScore($rankUserDayKeyMile,$val) :0;
                    $data['create_time'] = time();
                    $data['date'] = $formartYesterday;
                    $this->paomeDb->insertRow('r_pm_rank_day',$data,mysqldb::INSERT_COMMON);
                }
            }
        }
        if($roundId == 100) { //争霸赛新规
            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->轮次：同步积分排名START:'.PHP_EOL,3,$logPath);
            /*同步积分排名开始*/
            $list = $this->paomeDb->getAll("select id,total_score from r_pm_score where `type`= 100  order by total_score desc,speed asc,mileage desc,support_num desc");
            foreach ($list as $key=>$val) {
                $rankNum = $key+1;
                $dataScoreRank =array('score_temp_rank_num'=>$rankNum);
                if ($val['total_score'] > 0) {
                    $dataScoreRank['score_rank_num'] = $rankNum;
                }
                $setStr = common::arrayToString($dataScoreRank);
                error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->轮次：r_pm_score 同步积分排名-sql:'."update r_pm_score set $setStr where id={$val['id']}".PHP_EOL,3,$logPath);
                $this->paomeDb->update("update r_pm_score set $setStr where id={$val['id']}");
            }
            $list = $this->paomeDb->getAll("select id,total_score from r_pm_score_day where `type`= 100  and date='$formartYesterday' order by total_score desc,speed asc,mileage desc,support_num desc");
            foreach ($list as $key=>$val) {
                $rankNum = $key+1;
                $dataScoreRank =array('score_temp_rank_num'=>$rankNum);
                if ($val['total_score'] > 0) {
                    $dataScoreRank['score_rank_num'] = $rankNum;
                }
                $setStr = common::arrayToString($dataScoreRank);
                error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->轮次：r_pm_score_day 同步积分排名-sql:'."update r_pm_score_day set $setStr where id={$val['id']}".PHP_EOL,3,$logPath);
                $this->paomeDb->update("update r_pm_score_day set $setStr where id={$val['id']}");
            }
            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨3点半跑一次记录-->轮次：同步积分排名结束:'.PHP_EOL,3,$logPath);
            /*同步积分排名结束*/
        }
    }


    /**
     * cron
     */
    public function autoInitRank() {
        $arrInfo = $this->getCurrentPaomeInfo('id');
        if(!$arrInfo) {
           return false;
        }
        $paomeId = $arrInfo['id'];
        $date = date('Ymd');
        $rankCronAutoInitIsExistKey = 'rankIsExistCronAutoInit-'.$paomeId;
        $value = $this->redis->zScore($rankCronAutoInitIsExistKey, $date);
        if ($value > 0) {
            $logPath = $this->base->config['log-dir'].'api/addUserRunData-'.date('Y-m-d').'.log';
            error_log('时间'.date('Y-m-d H:i:s').' 每晚凌晨同步数据-->已经跑过一次'.PHP_EOL,3,$logPath);
            return false;
        }
        $this->syncRedisRankNum();
        $this->initTodayRankFromYesterdayData();
        $this->redis->zAdd($rankCronAutoInitIsExistKey, 1, $date);
    }

    /**
     *  上传跑步记录
     */
    public function addUserRunDataBak($did) {
        //$logPath = $this->base->config['log-dir'].'api/addUserRunData-'.date('Y-m-d').'.log';
        $logPath = $this->base->config['log-dir'].'api/addUserRunData-'.'.log';
        error_log('时间'.date('Y-m-d H:i:s').' start上传跑步记录:did'.$did.PHP_EOL,3,$logPath);
        //可以上传跑步记录的时间赛季状态
        $arrCanLoadStatus = common::$arrCanLoadStatus;//5,6,10,11,15,16,20,21,25,26,30,31,105,106
        $sql = "select * from r_pm_contest where flag=1 and status in(".implode(',',$arrCanLoadStatus).")";
        $info= $this->paomeDb->getRow($sql);//赛季时间不重叠
        if(!$info) {
            error_log('时间'.date('Y-m-d H:i:s').' end 不可以上传跑步记录:sql'.$sql.PHP_EOL,3,$logPath);
            return false;
        } //不在上传范围的时间内
        $paomeId = $info['id'];
        $paomeStatus = $info['status'];
        list($roundId, $roundId2) = $this->getCurrentRoundInfo($paomeStatus);
        error_log('时间'.date('Y-m-d H:i:s').' continue 查询轮次:$roundId：'.$roundId.'-$roundId2'.$roundId2.PHP_EOL,3,$logPath);
        if (!$roundId || !$roundId2) return false;

        //跑步数据加工
        $sql = "select did,updatetime,uid,mileage,elapsedtime from r_data where did = $did and overSpeed=0";
        $arrRunData = $this->paomeDb->getRow($sql);
        if (!$arrRunData){
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步数据加工:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;
        }
        $uid = $arrRunData['uid'];
        //俱乐部状态
        $sql = "select b.club_id from r_pm_club as a LEFT JOIN  r_pm_club_users as b ON a.id=b.club_id WHERE a.status=0 and b.uid=$uid and b.status=0";
        $arrUserClub = $this->paomeDb->getRow($sql);
        if (!$arrUserClub) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跟俱乐部关系:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;
        }
        $clubId = $arrUserClub['club_id'];
        //俱乐部当前赛季状态
        $sql = "select * from r_pm_club_status where contest_id=$paomeId and club_id=$clubId and type=$roundId2 and status in(15,20)";//竞赛中和空档期
        $arrClubStatus = $this->paomeDb->getRow($sql);
        if (!$arrClubStatus){
            error_log('时间'.date('Y-m-d H:i:s').' continue 俱乐部当前赛季状态:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;
        }

        //跑步记录有效性-跑步结束时间判断
        $arrValidStatus = common::$arrValidStatus;//跑步记录有效状态
        if (!isset($arrValidStatus[$roundId2])) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步记录有效性A:$roundId2：'.$roundId2.PHP_EOL,3,$logPath);
            return false;//无效
        }
        $time = $arrRunData['updatetime'];//跑步结束时间
        $sql = "select id from r_pm_rounds
                  where status =0
                    and $time >= start_time
                    and $time <= end_time
                    and `type` = $arrValidStatus[$roundId2]
                    and status=0";//同一轮，同一阶段且跑步记录有效状态,第一轮 第二阶段 上传第一阶段的信息，不行
        $arr = $this->paomeDb->getOne($sql);
        if (!$arr) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步记录有效性:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;//无效
        }

        //只要是今天上传的，都算今天的跑步记录.覆盖time值
        //$time = (date('Ymd',time()) != date('Ymd',$time)) ? time() : $time;
        $time = strtotime(date("Ymd",time()));//全部为0点，防止空档期产生的记录不能上传的情况

        $data = array(
            'contest_id'=>$paomeId,
            'uid'=>$uid,
            'did'=>$did,
            'club_id'=>$clubId,
            'create_time'=>$time
        );
        $r = $this->paomeDb->insertRow('r_pm_club_users_did',$data,0);
        if (!$r) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步记录下来:$data：'.var_export($data,true).PHP_EOL,3,$logPath);
            return false; //跑步记录失败
        }
        /*用户跑步轮次记录 start*/
        $rankUserRoundsIsExistKey = 'rankIsExistUserRounds-'.$paomeId.'-'.$roundId.'-'.$clubId.'-'.$uid;
        if ($this->redis->get($rankUserRoundsIsExistKey)){
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次-2等待开始:$did：'.$did.PHP_EOL,3,$logPath);
            sleep(3);//防重复
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次-2等待结束:$did：'.$did.PHP_EOL,3,$logPath);
        } else {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次-1:$did：'.$did.PHP_EOL,3,$logPath);
            $this->redis->setex($rankUserRoundsIsExistKey,3,1);
        }
        $mileage = $arrRunData['mileage'];
        $mileage_three = 0;
        $elapsedtime = 0;
        $speed =0;
        if ($mileage >=3) {
            $mileage_three += $arrRunData['mileage'];
            $elapsedtime += $arrRunData['elapsedtime'];
            $speed_1 = (($elapsedtime/$mileage_three)/60);//配速
            $num1 = floor($speed_1); //整数部分
            $num2  = $speed_1-$num1;
            $speed = bcadd($num1*60,floor($num2*60));//单位 s/km
        }
            $existList = $this->paomeDb->getRow("select * from r_pm_users_rounds
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId");
        $mileageUser = $mileage;
        $elapsedtimeUser = $elapsedtime;
        $speedUser = $speed;
        $maxDid = $did;
        $maxMileage = $mileage;
        $dayNum =1;
        if (!$existList) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次0:$data：'."select * from r_pm_users_rounds
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId".PHP_EOL,3,$logPath);
            $data = array(
                'club_id'=>$clubId,
                'contest_id'=>$paomeId,
                'uid'=>$uid,
                'type'=>$roundId,
                'mileage'=>$mileageUser,
                'speed'=>$speedUser,
                'max_did'=>$maxDid,
                'max_mileage'=>$maxMileage,
                'create_time'=>time(),
                'day_num'=>$dayNum,
                'date'=>date('Ymd',$time),
                'elapsedtime'=>$elapsedtimeUser,
                'mileage_three'=>$mileage_three
            );
            $r = $this->paomeDb->insertRow('r_pm_users_rounds',$data,0);

            $data = array(
                'club_id'=>$clubId,
                'contest_id'=>$paomeId,
                'uid'=>$uid,
                'type'=>$roundId2,
                'mileage'=>$mileageUser,
                'speed'=>$speedUser,
                'max_did'=>$maxDid,
                'max_mileage'=>$maxMileage,
                'create_time'=>time(),
                'day_num'=>$dayNum,
                'date'=>date('Ymd',$time),
                'elapsedtime'=>$elapsedtimeUser,
                'mileage_three'=>$mileage_three
            );
            $r = $this->paomeDb->insertRow('r_pm_users_status',$data,0);
        } else {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次1:$data：'."select * from r_pm_users_rounds
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId".PHP_EOL,3,$logPath);

            //查询历史日期记录是否存在
           // $startTimeUpdate = strtotime(date('Y-m-d' ,$time));
            //$endTimeUpdate = strtotime(date('Y-m-d 23:59:59'), $time);
            //$isExistUpload = $this->paomeDb->getOne("select id from r_pm_club_users_did where contest_id=$paomeId and uid=$uid and club_id=$clubId and create_time >=$startTimeUpdate and create_time<=$endTimeUpdate");
            if ($existList['date'] == date('Ymd')) {
                error_log('时间'.date('Y-m-d H:i:s').' continue DAY-1:$data：'.var_export($existList['date'].'-'.date('Ymd'),true).PHP_EOL,3,$logPath);
                $dayNum = $existList['day_num'];
            } else {
                error_log('时间'.date('Y-m-d H:i:s').' continue DAY-2:$data：'.var_export($existList['date'].'-'.date('Ymd'),true).PHP_EOL,3,$logPath);
                $dayNum = $existList['day_num']+1;
            }
            $mileageUser = $mileage+$existList['mileage'];
            $maxDid =($mileage > $existList['max_mileage']) ? $did :$existList['max_did'];
            $maxMileage =($mileage > $existList['max_mileage']) ? $mileage :$existList['max_mileage'];

            $mileage_three = $existList['mileage_three'];
            $elapsedtimeUser = $existList['elapsedtime'];
            $speedUser = $existList['speed'];

            if ($mileage >= 3) {
                $mileage_three += $mileage;
                $elapsedtimeUser += $elapsedtime;
                $speed_1 = (($elapsedtimeUser/$mileage_three)/60);//配速
                $num1 = floor($speed_1); //整数部分
                $num2  = $speed_1-$num1;
                $speedUser = bcadd($num1*60,floor($num2*60));//单位 s/km
            }
            $data = array(
                'mileage'=>$mileageUser,
                'speed'=>$speedUser,
                'max_did'=>$maxDid,
                'max_mileage'=>$maxMileage,
                'day_num'=>$dayNum,
                'date'=>date('Ymd', $time),
                'elapsedtime'=>$elapsedtimeUser,
                'mileage_three'=>$mileage_three
            );
            error_log('时间'.date('Y-m-d H:i:s').' continue DAY-3:$data：'.var_export($data,true).PHP_EOL,3,$logPath);
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_users_rounds set $setStr where id= {$existList['id']}";
            $r = $this->paomeDb->update($sql);

            //查询阶段记录
            $existListJd = $this->paomeDb->getRow("select * from r_pm_users_status
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId2");
            if (!$existListJd) {
                $data = array(
                    'club_id'=>$clubId,
                    'contest_id'=>$paomeId,
                    'uid'=>$uid,
                    'type'=>$roundId2,
                    'mileage'=>$mileageUser,
                    'speed'=>$speedUser,
                    'max_did'=>$maxDid,
                    'max_mileage'=>$maxMileage,
                    'create_time'=>$time,
                    'day_num'=>1,
                    'date'=>date('Ymd', $time),
                    'elapsedtime'=>$elapsedtimeUser,
                    'mileage_three'=>$mileage_three
                );
                $r = $this->paomeDb->insertRow('r_pm_users_status',$data,0);
            } else {
                $data =  array(
                    'mileage'=>$mileageUser,
                    'speed'=>$speedUser,
                    'max_did'=>$maxDid,
                    'max_mileage'=>$maxMileage,
                    'day_num'=>$dayNum,
                    'date'=>date('Ymd', $time),
                    'elapsedtime'=>$elapsedtimeUser,
                    'mileage_three'=>$mileage_three
                );
                $setStr = common::arrayToString($data);
                $sql = "update r_pm_users_status set $setStr where id= {$existListJd['id']}";
                $r = $this->paomeDb->update($sql);
            }
        }
        /*用户跑步轮次记录 end*/

        /*俱乐部跑步轮次记录 start*/
            $arrClubRound = $this->paomeDb->getRow("select * from r_pm_club_rounds where contest_id=$paomeId and club_id=$clubId and `type`=$roundId");
        if (!$arrClubRound) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 俱乐部跑步轮次记录:$data：'."select * from r_pm_club_rounds where contest_id=$paomeId and club_id=$clubId and `type`=>$roundId".PHP_EOL,3,$logPath);
            return false;
        }//异常

        $mileageClub = $arrClubRound['mileage']+$mileage;
        $mileageThreeClub = $arrClubRound['mileage_three'];
        $elapsedtimeClub = $arrClubRound['elapsedtime'];
        $speedClub = $arrClubRound['speed'];

        if($mileage >=3) {
            $mileageThreeClub += $mileage;
            $elapsedtimeClub += $elapsedtime;
            $speed_1 = (($elapsedtimeClub/$mileageThreeClub)/60);//配速
            $num1 = floor($speed_1); //整数部分
            $num2  = $speed_1-$num1;
            $speedClub = bcadd($num1*60,floor($num2*60));//单位 s/km
        }
        $data =  array(
            'mileage'=>$mileageClub,
            'speed'=>$speedClub,
            'elapsedtime'=>$elapsedtimeClub,
            'mileage_three'=>$mileageThreeClub
        );
        $setStr = common::arrayToString($data);
        $sql = "update r_pm_club_rounds set $setStr where id= {$arrClubRound['id']}";
        $r = $this->paomeDb->update($sql);

        //r_pm_club_status 变更
        $sql = "update r_pm_club_status set $setStr where id= {$arrClubStatus['id']}";
        $r = $this->paomeDb->update($sql);

        //r_pm_club_status_list变更
        $sql = "select * from r_pm_club_status_list where contest_id=$paomeId and club_id=$clubId and `type`=$roundId2 and status in(15,20)";//竞赛中和空档期
        $arrClubStatusList = $this->paomeDb->getRow($sql);
        if (!$arrClubStatusList) return false;//异常
        $sql = "update r_pm_club_status_list set $setStr where id= {$arrClubStatusList['id']}";
        $r = $this->paomeDb->update($sql);
        /*俱乐部跑步轮次记录 end*/

        /*排名 start*/
            //跑量
            $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
            $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
            $rankClubDayKeyMile = 'rankClubDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天
            $rankClubJdKeyMile= 'rankClubJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
            //配速
            $rankClubKeySpeed = 'rankClub-speed-'.$paomeId.'-'.$roundId;
            $rankClubDayKeySpeed = 'rankClubDay-speed-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天
            $rankClubJdKeySpeed = 'rankClubJd-speed-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
            //人气
            $rankClubKeySupportNum = 'rankCLub-supportNum-'.$paomeId.'-'.$roundId;
            $rankClubDayKeySupportNum = 'rankClubDay-supportNum-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天
            $rankClubJdKeySupportNum = 'rankClubJd-supportNum-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
            //全员跑量
            $rankUserKeyMile = 'rankUser-mileage-'.$paomeId.'-'.$roundId;
            $rankUserDayKeyMile = 'rankUserDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天
            $rankUserJdKeyMile = 'rankUserJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段

            $r11 = $this->redis->zAdd($rankClubKeyMile,$mileageClub,$clubId);
        if (!$r11) {
            error_log('时间'.date('Y-m-d H:i:s').' 俱乐部跑步轮次记录XXX：'.var_export($mileageClub.'-'.$clubId.'-'.$r11,true).PHP_EOL,3,$logPath);

        }
            if ($roundId == 100 || !$this->base->load('paome/rank')->getClubIsHaveTicket($paomeId,$clubId)) {//俱乐部是否取得过争霸赛资格,争霸赛不影响
                $this->redis->zAdd($rankClubKeyMileOk,$mileageClub,$clubId);
            }
            $this->redis->zAdd($rankClubDayKeyMile,$mileageClub,$clubId);
            $this->redis->zAdd($rankClubJdKeyMile,$mileageClub,$clubId);

        if($speedClub > 0) {
            $this->redis->zAdd($rankClubKeySpeed,$speedClub,$clubId);
            $this->redis->zAdd($rankClubDayKeySpeed,$speedClub,$clubId);
            $this->redis->zAdd($rankClubJdKeySpeed,$speedClub,$clubId);
        }


//            $this->redis->zAdd($rankClubKeySupportNum,$mileageNew,$clubId);
//            $this->redis->zAdd($rankClubDayKeySupportNum,$mileageNew,$clubId);
//            $this->redis->zAdd($rankClubJdKeySupportNum,$mileageNew,$clubId);

            $this->redis->zAdd($rankUserKeyMile,$mileageUser,$uid);
            $this->redis->zAdd($rankUserDayKeyMile,$mileageUser,$uid);
            $this->redis->zAdd($rankUserJdKeyMile,$mileageUser,$uid);

           //数据落地
        $arrRankMile = [];
        $arrRankDayMile = [];
        $arrRankJdMile = [];

        $arrRankSpeed = [];
        $arrRankDaySpeed = [];
        $arrRankJdSpeed = [];

        $arrRankUser = [];
        $arrRankDayUser = [];
        $arrRankJdUser = [];
        $existListRank = $this->paomeDb->getAll("select id,type_wd from r_pm_rank
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and `type` = $roundId
                                    and object_id =$clubId
                                    and type_wd in(1,5)
                                    and status=0");

        $existListRankDay = $this->paomeDb->getAll("select id,type_wd from r_pm_rank_day
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$clubId
                                    and `type` = $roundId
                                    and type_wd in(1,5)
                                    and `date` ='". date('Ymd',$time)
            ."' and status=0");
        error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次2:$data：'."select id,type_wd from r_pm_rank_day
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$clubId
                                    and `type` = $roundId
                                    and type_wd in(1,5)
                                    and `date` ='". date('Ymd',$time)
            ."' and status=0".PHP_EOL,3,$logPath);



        $existListRankJd = $this->paomeDb->getAll("select id,type_wd from r_pm_rank_jd
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$clubId
                                    and `type` = $roundId2
                                    and type_wd in(1,5)
                                    and status=0");
        /*全员跑量 start*/
        $arrRankUser = $this->paomeDb->getRow("select id,type_wd from r_pm_rank
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and `type` = $roundId
                                    and object_id =$uid
                                    and type_wd =15
                                    and status=0");

        $arrRankDayUser = $this->paomeDb->getRow("select id,type_wd from r_pm_rank_day
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$uid
                                    and `type` = $roundId
                                    and type_wd = 15
                                    and `date` ='". date('Ymd',$time)
            ."' and status=0");

        $arrRankJdUser = $this->paomeDb->getRow("select id,type_wd from r_pm_rank_jd
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$uid
                                    and `type` = $roundId2
                                    and type_wd = 15
                                    and status=0");
        /*全员跑量 end*/
        if ($existListRank) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次3:$data：'.var_export($existListRank, true).PHP_EOL,3,$logPath);
            foreach ($existListRank as $key=>$val) {
                if ($val['type_wd'] == 1) {
                    $arrRankMile = $val;
                } else if($val['type_wd'] == 5) {
                    $arrRankSpeed = $val;
                }
            }
        }

        if($existListRankDay) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次4:$data：'.var_export($existListRank, true).PHP_EOL,3,$logPath);
            foreach ($existListRankDay as $key=>$val) {
                if ($val['type_wd'] == 1) {
                    $arrRankDayMile = $val;
                } else if($val['type_wd'] == 5) {
                    $arrRankDaySpeed = $val;
                }
            }
        }

        if($existListRankJd) {
            foreach ($existListRankJd as $key=>$val) {
                if ($val['type_wd'] == 1) {
                    $arrRankJdMile = $val;
                } else if($val['type_wd'] == 5) {
                    $arrRankJdSpeed = $val;
                }
            }
        }
      /*里程排名 start*/
        if (!$arrRankMile) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>1,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank set $setStr where id= {$arrRankMile['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankDayMile) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>1,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageClub,
                'date'=>date('Ymd',$time)
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_day',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_day set $setStr where id= {$arrRankDayMile['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankJdMile) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId2,
                'type_wd'=>1,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_jd',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_jd set $setStr where id= {$arrRankJdMile['id']}";
            $r = $this->paomeDb->update($sql);
        }
        /*里程排名end*/

        /*配速排名记录 start*/
        if (!$arrRankSpeed) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>5,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$speedClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$speedClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank set $setStr where id= {$arrRankSpeed['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankDaySpeed) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>5,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$speedClub,
                'date'=>date('Ymd',$time)
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_day',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$speedClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_day set $setStr where id= {$arrRankDaySpeed['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankJdSpeed) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId2,
                'type_wd'=>5,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$speedClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_jd',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$speedClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_jd set $setStr where id= {$arrRankJdSpeed['id']}";
            $r = $this->paomeDb->update($sql);
        }
        /*配速排名记录 end*/

        /*全员跑量排名 start*/
        if (!$arrRankUser) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>15,
                'object_id'=>$uid,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageUser
            );
            $r = $this->paomeDb->insertRow('r_pm_rank',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageUser
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank set $setStr where id= {$arrRankUser['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankDayUser) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>15,
                'object_id'=>$uid,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageUser,
                'date'=>date('Ymd',$time)
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_day',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageUser
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_day set $setStr where id= {$arrRankDayUser['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankJdUser) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId2,
                'type_wd'=>15,
                'object_id'=>$uid,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageUser
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_jd',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageUser
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_jd set $setStr where id= {$arrRankJdUser['id']}";
            $r = $this->paomeDb->update($sql);
        }

        error_log('时间'.date('Y-m-d H:i:s').' END 用户跑步上传结束 did:'.$did.PHP_EOL,3,$logPath);
        /*全员跑量排名 end*/
        /*排名 end*/
    }

    /**
     *  上传跑步记录
     */
    public function addUserRunData($did) {
        //$logPath = $this->base->config['log-dir'].'api/addUserRunData-'.date('Y-m-d').'.log';
        $logPath = $this->base->config['log-dir'].'api/addUserRunData-'.'.log';
        error_log('时间'.date('Y-m-d H:i:s').' start上传跑步记录:did'.$did.PHP_EOL,3,$logPath);
        //可以上传跑步记录的时间赛季状态
        $arrCanLoadStatus = common::$arrCanLoadStatus;//5,6,10,11,15,16,20,21,25,26,30,31,105,106
        $sql = "select * from r_pm_contest where flag=1 and status in(".implode(',',$arrCanLoadStatus).")";
        $info= $this->paomeDb->getRow($sql);//赛季时间不重叠
        if(!$info) {
            error_log('时间'.date('Y-m-d H:i:s').' end 不可以上传跑步记录:sql'.$sql.PHP_EOL,3,$logPath);
            return false;
        } //不在上传范围的时间内
        $paomeId = $info['id'];
        $paomeStatus = $info['status'];
        list($roundId, $roundId2) = $this->getCurrentRoundInfo($paomeStatus);
        error_log('时间'.date('Y-m-d H:i:s').' continue 查询轮次:$roundId：'.$roundId.'-$roundId2'.$roundId2.PHP_EOL,3,$logPath);
        if (!$roundId || !$roundId2) return false;

        //跑步数据加工
        $sql = "select did,updatetime,uid,mileage,elapsedtime from r_data where did = $did and overSpeed=0";
        $arrRunData = $this->paomeDb->getRow($sql);
        if (!$arrRunData){
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步数据加工:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;
        }
        /*记录跑步记录需要记录原始信息*/
        $oldMileage = $arrRunData['mileage'];
        $oldElapsedtime = $arrRunData['elapsedtime'];
        //争霸赛新规 start
        if($roundId == 100) {
            /*-排除低于3km start*/
            if($arrRunData['mileage'] < self::USER_ONCE_MIN_MILEAGE) {
                error_log('时间'.date('Y-m-d H:i:s').' continue 跑步里程小于3km:mileage：'.$arrRunData['mileage'].'-did'.$did.PHP_EOL,3,$logPath);
                return false;
            }
            /*-排除低于3km end*/
            /*排除配速大于10min start*/
            $onceSpeed = $this->getSecSpeed($arrRunData['mileage'], $arrRunData['elapsedtime']);
            if ($onceSpeed >= self::USER_ONCE_MAX_SPEED) {
                error_log('时间'.date('Y-m-d H:i:s').' continue 跑步配速大于10min:mileage：'.$onceSpeed.'-did'.$did.PHP_EOL,3,$logPath);
                return false;
            }
            /*排除配速大于10min end*/

        }
        //争霸赛新规end

        $uid = $arrRunData['uid'];
        //俱乐部状态
        $sql = "select b.club_id from r_pm_club as a LEFT JOIN  r_pm_club_users as b ON a.id=b.club_id WHERE a.status=0 and b.uid=$uid and b.status=0";
        $arrUserClub = $this->paomeDb->getRow($sql);
        if (!$arrUserClub) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跟俱乐部关系:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;
        }
        $clubId = $arrUserClub['club_id'];
        //俱乐部当前赛季状态
        $sql = "select * from r_pm_club_status where contest_id=$paomeId and club_id=$clubId and type=$roundId2 and status in(15,20)";//竞赛中和空档期
        $arrClubStatus = $this->paomeDb->getRow($sql);
        if (!$arrClubStatus){
            error_log('时间'.date('Y-m-d H:i:s').' continue 俱乐部当前赛季状态:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;
        }

        //跑步记录有效性-跑步结束时间判断
        $arrValidStatus = common::$arrValidStatus;//跑步记录有效状态
        if (!isset($arrValidStatus[$roundId2])) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步记录有效性A:$roundId2：'.$roundId2.PHP_EOL,3,$logPath);
            return false;//无效
        }
        $time = $arrRunData['updatetime'];//跑步结束时间
        $sql = "select id from r_pm_rounds
                  where status =0
                    and $time >= start_time
                    and $time <= end_time
                    and `type` = $arrValidStatus[$roundId2]
                    and status=0";//同一轮，同一阶段且跑步记录有效状态,第一轮 第二阶段 上传第一阶段的信息，不行
        $arr = $this->paomeDb->getOne($sql);
        if (!$arr) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步记录有效性:$sql：'.$sql.PHP_EOL,3,$logPath);
            return false;//无效
        }

        /*用户跑步轮次记录 start*/
        $rankUserRoundsIsExistKey = 'rankIsExistUserRounds-'.$paomeId.'-'.$roundId.'-'.$clubId.'-'.$uid;
        if ($this->redis->get($rankUserRoundsIsExistKey)){
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次-2等待开始:$did：'.$did.'-UID'.$uid.'-时间'.date('Y-m-d H:i:s').PHP_EOL,3,$logPath);
            sleep(3);//防重复
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次-2等待结束:$did：'.$did.'-UID'.$uid.'-时间'.date('Y-m-d H:i:s').PHP_EOL,3,$logPath);
        } else {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次-1:$did：'.$did.'-UID'.$uid.'-时间'.date('Y-m-d H:i:s').PHP_EOL,3,$logPath);
            $this->redis->setex($rankUserRoundsIsExistKey,3,1);
        }
        /*用户当天上传总里程*/
        $unixTime = time();
        list($userCurrentDayMileage, $userCurrentDayElapsedtime) = $this->getUserCurrentDayMileage($paomeId, $clubId, $uid, date("Ymd", $unixTime));
        $userCurrentDayMileage = (double)$userCurrentDayMileage;
        //争霸赛新规 start
        if ($roundId == 100) {
            /*单人一天只能上传45km start*/
                if ($userCurrentDayMileage >= self::USER_DAY_MAX_MILEAGE) {
                    error_log('时间'.date('Y-m-d H:i:s').' continue 单人一天只能上传45km:$did：'.$did.'-UID'.$uid.'-时间'.date('Y-m-d H:i:s').PHP_EOL,3,$logPath);
                    return false;
                }
                if ( ($userCurrentDayMileage + $arrRunData['mileage']) > self::USER_DAY_MAX_MILEAGE) {//截取公里数，截取时间
                     $syMileage = self::USER_DAY_MAX_MILEAGE-$userCurrentDayMileage;//截取需要补充的公里数
                     $syElapsedtime = ($syMileage*$arrRunData['elapsedtime'])/$arrRunData['mileage'];//截取公里数花费平均时间
                     $arrRunData['mileage'] = $syMileage;//重写公里
                     $arrRunData['elapsedtime'] = $syElapsedtime;//重写时间
                    error_log('时间'.date('Y-m-d H:i:s').' continue 单人一天上传45km，重写数据:$did：'.$did.'-UID'.$uid.'-时间'.date('Y-m-d H:i:s').'-mileage:'.$syMileage.'-elapsedtime:'.$syElapsedtime.PHP_EOL,3,$logPath);
                }
            /*单人一天只能上传45km end*/
        }
        //争霸赛新规 end

        //只要是今天上传的，都算今天的跑步记录.覆盖time值
        $time = strtotime(date("Ymd",$unixTime));//全部为0点，防止空档期产生的记录不能上传的情况
        $data = array(
            'contest_id'=>$paomeId,
            'uid'=>$uid,
            'did'=>$did,
            'club_id'=>$clubId,
            'create_time'=>$time,
            'mileage'=>$oldMileage,
            'elapsedtime'=>$oldElapsedtime,
            'new_mileage'=>$arrRunData['mileage'],
            'new_elapsedtime'=>$arrRunData['elapsedtime']
        );
        $r = $this->paomeDb->insertRow('r_pm_club_users_did',$data,0);
        if (!$r) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 跑步记录下来:$data：'.var_export($data,true).PHP_EOL,3,$logPath);
            return false; //跑步记录失败
        }

        $mileage = $arrRunData['mileage'];
        $mileage_three = 0;
        $elapsedtime = 0;
        $speed =0;
        if ($mileage >=self::USER_ONCE_MIN_MILEAGE) {
            $mileage_three += $arrRunData['mileage'];
            $elapsedtime += $arrRunData['elapsedtime'];
            $speed = $this->getSecSpeed($mileage_three, $elapsedtime);//单位 s/km
        }

        $mileageUser = $mileage;
        $elapsedtimeUser = $elapsedtime;
        $speedUser = $speed;
        $maxDid = $did;
        $maxMileage = $mileage;
        $dayNum =1;
        $existList = $this->paomeDb->getRow("select * from r_pm_users_rounds
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId");
        if (!$existList) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次0:$data：'."select * from r_pm_users_rounds
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId".PHP_EOL,3,$logPath);
            $data = array(
                'club_id'=>$clubId,
                'contest_id'=>$paomeId,
                'uid'=>$uid,
                'type'=>$roundId,
                'mileage'=>$mileageUser,
                'speed'=>$speedUser,
                'max_did'=>$maxDid,
                'max_mileage'=>$maxMileage,
                'create_time'=>$unixTime,
                'day_num'=>$dayNum,
                'date'=>date('Ymd',$time),
                'elapsedtime'=>$elapsedtimeUser,
                'mileage_three'=>$mileage_three
            );
            $r = $this->paomeDb->insertRow('r_pm_users_rounds',$data,0);

            $data = array(
                'club_id'=>$clubId,
                'contest_id'=>$paomeId,
                'uid'=>$uid,
                'type'=>$roundId2,
                'mileage'=>$mileageUser,
                'speed'=>$speedUser,
                'max_did'=>$maxDid,
                'max_mileage'=>$maxMileage,
                'create_time'=>$unixTime,
                'day_num'=>$dayNum,
                'date'=>date('Ymd',$time),
                'elapsedtime'=>$elapsedtimeUser,
                'mileage_three'=>$mileage_three
            );
            $r = $this->paomeDb->insertRow('r_pm_users_status',$data,0);
        } else {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次1:$data：'."select * from r_pm_users_rounds
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId".PHP_EOL,3,$logPath);

            //查询历史日期记录是否存在
            // $startTimeUpdate = strtotime(date('Y-m-d' ,$time));
            //$endTimeUpdate = strtotime(date('Y-m-d 23:59:59'), $time);
            //$isExistUpload = $this->paomeDb->getOne("select id from r_pm_club_users_did where contest_id=$paomeId and uid=$uid and club_id=$clubId and create_time >=$startTimeUpdate and create_time<=$endTimeUpdate");
            if ($existList['date'] == date('Ymd')) {
                error_log('时间'.date('Y-m-d H:i:s').' continue DAY-1:$data：'.var_export($existList['date'].'-'.date('Ymd'),true).PHP_EOL,3,$logPath);
                $dayNum = $existList['day_num'];
            } else {
                error_log('时间'.date('Y-m-d H:i:s').' continue DAY-2:$data：'.var_export($existList['date'].'-'.date('Ymd'),true).PHP_EOL,3,$logPath);
                $dayNum = $existList['day_num']+1;
            }
            $mileageUser = $mileage+$existList['mileage'];
            $maxDid =($mileage > $existList['max_mileage']) ? $did :$existList['max_did'];
            $maxMileage =($mileage > $existList['max_mileage']) ? $mileage :$existList['max_mileage'];

            $mileage_three = $existList['mileage_three'];
            $elapsedtimeUser = $existList['elapsedtime'];
            $speedUser = $existList['speed'];

            if ($mileage >= self::USER_ONCE_MIN_MILEAGE) {
                $mileage_three += $mileage;
                $elapsedtimeUser += $elapsedtime;
                $speedUser = $this->getSecSpeed($mileage_three, $elapsedtimeUser);//单位 s/km
            }
            $data = array(
                'mileage'=>$mileageUser,
                'speed'=>$speedUser,
                'max_did'=>$maxDid,
                'max_mileage'=>$maxMileage,
                'day_num'=>$dayNum,
                'date'=>date('Ymd', $time),
                'elapsedtime'=>$elapsedtimeUser,
                'mileage_three'=>$mileage_three
            );
            error_log('时间'.date('Y-m-d H:i:s').' continue DAY-3:$data：'.var_export($data,true).PHP_EOL,3,$logPath);
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_users_rounds set $setStr where id= {$existList['id']}";
            $r = $this->paomeDb->update($sql);

            //查询阶段记录
            $existListJd = $this->paomeDb->getRow("select * from r_pm_users_status
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and uid=$uid
                                    and `type`=$roundId2");
            if (!$existListJd) {
                $data = array(
                    'club_id'=>$clubId,
                    'contest_id'=>$paomeId,
                    'uid'=>$uid,
                    'type'=>$roundId2,
                    'mileage'=>$mileageUser,
                    'speed'=>$speedUser,
                    'max_did'=>$maxDid,
                    'max_mileage'=>$maxMileage,
                    'create_time'=>$time,
                    'day_num'=>1,
                    'date'=>date('Ymd', $time),
                    'elapsedtime'=>$elapsedtimeUser,
                    'mileage_three'=>$mileage_three
                );
                $r = $this->paomeDb->insertRow('r_pm_users_status',$data,0);
            } else {
                $data =  array(
                    'mileage'=>$mileageUser,
                    'speed'=>$speedUser,
                    'max_did'=>$maxDid,
                    'max_mileage'=>$maxMileage,
                    'day_num'=>$dayNum,
                    'date'=>date('Ymd', $time),
                    'elapsedtime'=>$elapsedtimeUser,
                    'mileage_three'=>$mileage_three
                );
                $setStr = common::arrayToString($data);
                $sql = "update r_pm_users_status set $setStr where id= {$existListJd['id']}";
                $r = $this->paomeDb->update($sql);
            }
        }
        /*用户跑步轮次记录 end*/

        /*俱乐部跑步轮次记录 start*/
        $arrClubRound = $this->paomeDb->getRow("select * from r_pm_club_rounds where contest_id=$paomeId and club_id=$clubId and `type`=$roundId");
        if (!$arrClubRound) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 俱乐部跑步轮次记录:$data：'."select * from r_pm_club_rounds where contest_id=$paomeId and club_id=$clubId and `type`=>$roundId".PHP_EOL,3,$logPath);
            return false;
        }//异常

        $mileageClub = $arrClubRound['mileage']+$mileage;
        $mileageThreeClub = $arrClubRound['mileage_three'];
        $elapsedtimeClub = $arrClubRound['elapsedtime'];
        $speedClub = $arrClubRound['speed'];

        if($mileage >=self::USER_ONCE_MIN_MILEAGE) {
            $mileageThreeClub += $mileage;
            $elapsedtimeClub += $elapsedtime;
            $speedClub = $this->getSecSpeed($mileageThreeClub, $elapsedtimeClub);//单位 s/km
        }
        $data =  array(
            'mileage'=>$mileageClub,
            'speed'=>$speedClub,
            'elapsedtime'=>$elapsedtimeClub,
            'mileage_three'=>$mileageThreeClub
        );
        $setStr = common::arrayToString($data);
        $sql = "update r_pm_club_rounds set $setStr where id= {$arrClubRound['id']}";
        $r = $this->paomeDb->update($sql);

        //r_pm_club_status 变更
        $sql = "update r_pm_club_status set $setStr where id= {$arrClubStatus['id']}";
        $r = $this->paomeDb->update($sql);

        //r_pm_club_status_list变更
        $sql = "select * from r_pm_club_status_list where contest_id=$paomeId and club_id=$clubId and `type`=$roundId2 and status in(15,20)";//竞赛中和空档期
        $arrClubStatusList = $this->paomeDb->getRow($sql);
        if (!$arrClubStatusList) return false;//异常
        $sql = "update r_pm_club_status_list set $setStr where id= {$arrClubStatusList['id']}";
        $r = $this->paomeDb->update($sql);
        /*俱乐部跑步轮次记录 end*/

        /*排名 start*/
        //跑量
        $rankClubKeyMile = 'rankClub-mileage-'.$paomeId.'-'.$roundId;//页面展示排名
        $rankClubKeyMileOk = 'rankClub-mileageOk-'.$paomeId.'-'.$roundId;//第一轮冠军不参与第二轮晋级争霸赛排名，决赛计算用到
        $rankClubDayKeyMile = 'rankClubDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天（争霸赛：用于查socre非排名）
        $rankClubDayKeyMileNew = 'rankClubDay-mileageNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天（争霸赛：用于查排名非score）
        $rankClubJdKeyMile= 'rankClubJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
        //配速
        $rankClubKeySpeed = 'rankClub-speed-'.$paomeId.'-'.$roundId;
        $rankClubDayKeySpeed = 'rankClubDay-speed-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天(争霸赛：用于查score非排名)
        $rankClubDayKeySpeedNew = 'rankClubDay-speedNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天(争霸赛：用于查排名非score)
        $rankClubJdKeySpeed = 'rankClubJd-speed-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
        //人气
        $rankClubKeySupportNum = 'rankCLub-supportNum-'.$paomeId.'-'.$roundId;
        $rankClubDayKeySupportNum = 'rankClubDay-supportNum-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天
        $rankClubJdKeySupportNum = 'rankClubJd-supportNum-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段
        //全员跑量
        $rankUserKeyMile = 'rankUser-mileage-'.$paomeId.'-'.$roundId;
        $rankUserDayKeyMile = 'rankUserDay-mileage-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$time);//天
        $rankUserJdKeyMile = 'rankUserJd-mileage-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段

        $r11 = $this->redis->zAdd($rankClubKeyMile,$mileageClub,$clubId);//轮次总里程
        if ($roundId == 100 || !$this->base->load('paome/rank')->getClubIsHaveTicket($paomeId,$clubId)) {//俱乐部是否取得过争霸赛资格,争霸赛不影响
            $this->redis->zAdd($rankClubKeyMileOk,$mileageClub,$clubId);
        }
        $this->redis->zAdd($rankClubJdKeyMile,$mileageClub,$clubId);//轮次阶段总里程
        if($speedClub > 0) {
            $this->redis->zAdd($rankClubKeySpeed, $speedClub, $clubId);//轮次总配速
            $this->redis->zAdd($rankClubJdKeySpeed, $speedClub, $clubId);//轮次阶段总配速
        }
        if ($roundId == 100) {
            //争霸赛新规 start
             /*战队当天的跑量和用时*/
            list($clubCurrentDayMileage, $clubCurrentDayElapsedtime) = $this->getClubCurrentDayMileage($paomeId, $clubId, date("Ymd", $unixTime));
            $clubCurrentDaySpeed = 0;
            //当天跑量用户>=5人才算配速
            $dayRunUserNum = $this->base->load('paome/cup')->getCurrentDayRunUserCnt($paomeId, $clubId, date("Ymd", $unixTime));
            if($dayRunUserNum >=self::CLUB_SPEED_MIN_USER_NUM) {
                $clubCurrentDaySpeed = $this->getSecSpeed($clubCurrentDayMileage, $clubCurrentDayElapsedtime);
            }

            $this->redis->zAdd($rankClubDayKeyMile,$clubCurrentDayMileage,$clubId);//轮次天里程(当天),用于获取score非排名
            $newMileRankValue = $this->getFormatSortValue($clubCurrentDayMileage, $clubCurrentDaySpeed, 1);
            $this->redis->zAdd($rankClubDayKeyMileNew, $newMileRankValue, $clubId);//天里程用于排名(里程从多到少，配速从快到慢)

            if($clubCurrentDaySpeed > 0) {
                $this->redis->zAdd($rankClubDayKeySpeed,$clubCurrentDaySpeed,$clubId);//轮次天配速（当天），用户获取score非排名
                $newSpeedRankValue = $this->getFormatSortValue($clubCurrentDayMileage, $clubCurrentDaySpeed, 5);
                $this->redis->zAdd($rankClubDayKeySpeedNew, $newSpeedRankValue, $clubId);//天配速用于排名(配速从快到慢，里程从多到少)
            }
            //争霸赛新规 end
        } else {
            $this->redis->zAdd($rankClubDayKeyMile,$mileageClub,$clubId);//轮次天里程(累计)
            if($speedClub > 0) {
                $this->redis->zAdd($rankClubDayKeySpeed,$speedClub,$clubId);//轮次天配速（累计）
            }
        }

//            $this->redis->zAdd($rankClubKeySupportNum,$mileageNew,$clubId);
//            $this->redis->zAdd($rankClubDayKeySupportNum,$mileageNew,$clubId);
//            $this->redis->zAdd($rankClubJdKeySupportNum,$mileageNew,$clubId);

        $this->redis->zAdd($rankUserKeyMile,$mileageUser,$uid);
        $this->redis->zAdd($rankUserDayKeyMile,$mileageUser,$uid);
        $this->redis->zAdd($rankUserJdKeyMile,$mileageUser,$uid);

        //排行数据落地
        $arrRankMile = [];
        $arrRankDayMile = [];
        $arrRankJdMile = [];

        $arrRankSpeed = [];
        $arrRankDaySpeed = [];
        $arrRankJdSpeed = [];

        $arrRankUser = [];
        $arrRankDayUser = [];
        $arrRankJdUser = [];
        $existListRank = $this->paomeDb->getAll("select id,type_wd from r_pm_rank
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and `type` = $roundId
                                    and object_id =$clubId
                                    and type_wd in(1,5)
                                    and status=0");

        $existListRankDay = $this->paomeDb->getAll("select id,type_wd from r_pm_rank_day
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$clubId
                                    and `type` = $roundId
                                    and type_wd in(1,5)
                                    and `date` ='". date('Ymd',$time)
            ."' and status=0");
        error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次2:$data：'."select id,type_wd from r_pm_rank_day
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$clubId
                                    and `type` = $roundId
                                    and type_wd in(1,5)
                                    and `date` ='". date('Ymd',$time)
            ."' and status=0".PHP_EOL,3,$logPath);



        $existListRankJd = $this->paomeDb->getAll("select id,type_wd from r_pm_rank_jd
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$clubId
                                    and `type` = $roundId2
                                    and type_wd in(1,5)
                                    and status=0");
        /*全员跑量 start*/
        $arrRankUser = $this->paomeDb->getRow("select id,type_wd from r_pm_rank
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and `type` = $roundId
                                    and object_id =$uid
                                    and type_wd =15
                                    and status=0");

        $arrRankDayUser = $this->paomeDb->getRow("select id,type_wd from r_pm_rank_day
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$uid
                                    and `type` = $roundId
                                    and type_wd = 15
                                    and `date` ='". date('Ymd',$time)
            ."' and status=0");

        $arrRankJdUser = $this->paomeDb->getRow("select id,type_wd from r_pm_rank_jd
                                    where contest_id=$paomeId
                                    and club_id=$clubId
                                    and object_id =$uid
                                    and `type` = $roundId2
                                    and type_wd = 15
                                    and status=0");
        /*全员跑量 end*/
        if ($existListRank) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次3:$data：'.var_export($existListRank, true).PHP_EOL,3,$logPath);
            foreach ($existListRank as $key=>$val) {
                if ($val['type_wd'] == 1) {
                    $arrRankMile = $val;
                } else if($val['type_wd'] == 5) {
                    $arrRankSpeed = $val;
                }
            }
        }

        if($existListRankDay) {
            error_log('时间'.date('Y-m-d H:i:s').' continue 用户跑步轮次4:$data：'.var_export($existListRank, true).PHP_EOL,3,$logPath);
            foreach ($existListRankDay as $key=>$val) {
                if ($val['type_wd'] == 1) {
                    $arrRankDayMile = $val;
                } else if($val['type_wd'] == 5) {
                    $arrRankDaySpeed = $val;
                }
            }
        }

        if($existListRankJd) {
            foreach ($existListRankJd as $key=>$val) {
                if ($val['type_wd'] == 1) {
                    $arrRankJdMile = $val;
                } else if($val['type_wd'] == 5) {
                    $arrRankJdSpeed = $val;
                }
            }
        }
        /*里程排名 start*/
        if (!$arrRankMile) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>1,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank set $setStr where id= {$arrRankMile['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankDayMile) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>1,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageClub,
                'date'=>date('Ymd',$time)
            );
            if ($roundId == 100){
                $data['rank_value'] = $clubCurrentDayMileage;
            }
            $r = $this->paomeDb->insertRow('r_pm_rank_day',$data,0);
        } else {
            if ($roundId == 100){
                $data =  array(
                    'rank_value'=>$clubCurrentDayMileage
                );
            } else {
                $data =  array(
                    'rank_value'=>$mileageClub
                );
            }
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_day set $setStr where id= {$arrRankDayMile['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankJdMile) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId2,
                'type_wd'=>1,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_jd',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_jd set $setStr where id= {$arrRankJdMile['id']}";
            $r = $this->paomeDb->update($sql);
        }
        /*里程排名end*/

        /*配速排名记录 start*/
        if (!$arrRankSpeed) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>5,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$speedClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$speedClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank set $setStr where id= {$arrRankSpeed['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankDaySpeed) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>5,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$speedClub,
                'date'=>date('Ymd',$time)
            );
            if ($roundId == 100){
                $data['rank_value'] = $clubCurrentDaySpeed;
            }
            $r = $this->paomeDb->insertRow('r_pm_rank_day',$data,0);
        } else {
            if ($roundId == 100) {
                $data =  array(
                    'rank_value'=>$clubCurrentDaySpeed
                );
            } else {
                $data =  array(
                    'rank_value'=>$speedClub
                );
            }
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_day set $setStr where id= {$arrRankDaySpeed['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankJdSpeed) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId2,
                'type_wd'=>5,
                'object_id'=>$clubId,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$speedClub
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_jd',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$speedClub
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_jd set $setStr where id= {$arrRankJdSpeed['id']}";
            $r = $this->paomeDb->update($sql);
        }
        /*配速排名记录 end*/

        /*全员跑量排名 start*/
        if (!$arrRankUser) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>15,
                'object_id'=>$uid,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageUser
            );
            $r = $this->paomeDb->insertRow('r_pm_rank',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageUser
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank set $setStr where id= {$arrRankUser['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankDayUser) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId,
                'type_wd'=>15,
                'object_id'=>$uid,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageUser,
                'date'=>date('Ymd',$time)
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_day',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageUser
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_day set $setStr where id= {$arrRankDayUser['id']}";
            $r = $this->paomeDb->update($sql);
        }

        if (!$arrRankJdUser) {
            $data = array(
                'contest_id'=>$paomeId,
                'club_id'=>$clubId,
                'type'=>$roundId2,
                'type_wd'=>15,
                'object_id'=>$uid,
                'status'=>0,
                'create_time'=>time(),
                'rank_value'=>$mileageUser
            );
            $r = $this->paomeDb->insertRow('r_pm_rank_jd',$data,0);
        } else {
            $data =  array(
                'rank_value'=>$mileageUser
            );
            $setStr = common::arrayToString($data);
            $sql = "update r_pm_rank_jd set $setStr where id= {$arrRankJdUser['id']}";
            $r = $this->paomeDb->update($sql);
        }

        error_log('时间'.date('Y-m-d H:i:s').' END 用户跑步上传结束 did:'.$did.PHP_EOL,3,$logPath);
        /*全员跑量排名 end*/
        /*排名 end*/
    }

    /**
     *  统计竞赛中的俱乐部正能量和点赞
     *  同步到跑么杯用户和俱乐部人气，以及排名
     * @param $type   1 正能量   2 点赞
     */
    public function addSupportNum($uid, $supportNum, $type=2) {
        $now = time();
        $info = $this->paomeDb->getRow("select contest_id,`type` from r_pm_rounds
                                                  where start_time<=$now
                                                  and end_time>=$now
                                                  and status=0");
        if(!$info) return false;//比赛结束 不能送人气
        $paomeId = $info['contest_id'];
        $arrClubInfo = $this->base->load('paome/paomeClub')->getUserCurrentClubInfo($uid);
        if (!$arrClubInfo) return false;
        $paomeStatus = $info['type'];
        //能点送人气的状态
        $roundId =0;
        $roundId2 = 0;
        $roundsDict = array(
            1 => array(1,5, 6, 10, 11, 12),
            5 => array(15, 16, 20, 21, 22),
            10 => array(25, 26, 30, 31,32),
            100 => array(105,106)
        );
        foreach($roundsDict as $key=>$val) {
            if (in_array($paomeStatus, $val)) {
                $roundId = $key;
                break;
            }
        }
        $roundsDict2 = array(
            1=>array(1,5,6),
            5=>array(10,11,12),
            10=>array(15,16),
            15=>array(20,21,22),
            20=>array(25,26),
            25=>array(30,31,32),
            100=>array(105,106)
        );
        foreach($roundsDict2 as $key=>$val) {
            if (in_array($paomeStatus, $val)) {
                $roundId2 = $key;
                break;
            }
        }
        if (!$roundId || !$roundId2) return false;
//        $isInGame = $this->paomeDb->getRow("select id from r_pm_club_status where contest_id=$paomeId and club_id={$arrClubInfo['id']} and `type`=$roundId2 and status=15");//是否在比赛中
//        if(!$isInGame) return false;
        $energyNum = 0;
        $agreeNum =0;
        if ($type == 1) {//正能量
            $energyNum = $supportNum;
        } else {//点赞
            $agreeNum = $supportNum;
        }
        $arrUserRound = $this->paomeDb->getRow("select id from r_pm_users_rounds where contest_id=$paomeId and club_id={$arrClubInfo['id']} and uid=$uid and `type`=$roundId");
        $data =[];
        if (!$arrUserRound) {//人气不需要记录日期 date ，不影响跑量天数
            $data['club_id'] = $arrClubInfo['id'];
            $data['contest_id'] = $paomeId;
            $data['uid'] = $uid;
            $data['type'] = $roundId;
            $data['support_num'] = $supportNum;
            $data['create_time'] = time();
            $data['energy_num'] = $energyNum;
            $data['agree_num'] = $agreeNum;
            //$data['support_user_num'] = time();  //todo 暂放
            $this->paomeDb->insertRow('r_pm_users_rounds',$data,mysqldb::INSERT_COMMON);
        } else {
            $this->paomeDb->update("update r_pm_users_rounds set support_num=support_num+$supportNum,energy_num = energy_num+$energyNum,agree_num=agree_num+$agreeNum where id={$arrUserRound['id']}");
        }

        //更新用户 r_pm_users_status
        $arrUserRoundStatus = $this->paomeDb->getRow("select id,type from r_pm_users_status where contest_id=$paomeId and club_id={$arrClubInfo['id']} and uid=$uid and `type`=$roundId2 ");
        $data =[];
        if (!$arrUserRoundStatus) {//人气不需要记录日期 date ，不影响跑量天数
            $data['club_id'] = $arrClubInfo['id'];
            $data['contest_id'] = $paomeId;
            $data['uid'] = $uid;
            $data['type'] = $roundId2;
            $data['support_num'] = $supportNum;
            $data['create_time'] = time();
            $data['energy_num'] = $energyNum;
            $data['agree_num'] = $agreeNum;
            //$data['support_user_num'] = time();  //todo 暂放
            $this->paomeDb->insertRow('r_pm_users_status',$data,mysqldb::INSERT_COMMON);
        } else {
            $this->paomeDb->update("update r_pm_users_status set support_num=support_num+$supportNum,energy_num = energy_num+$energyNum,agree_num=agree_num+$agreeNum where id={$arrUserRoundStatus['id']}");
        }

        //是否在比赛中
        $isInGame = $this->paomeDb->getOne("select id from r_pm_club_status where contest_id=$paomeId and club_id={$arrClubInfo['id']} and `type`=$roundId2 and status=15");
        if (!in_array($info['type'],array(5,10,15,20,25,30,105)) || !$isInGame) {//非比赛中, 比赛中才同步到战队人气及排名
            return false;
        }

        //同步俱乐部轮次表 r_pm_club_rounds
        $clubRoundId = $this->paomeDb->getOne("select id from r_pm_club_rounds where contest_id=$paomeId and club_id={$arrClubInfo['id']} and `type`=$roundId");
        if ($clubRoundId) {//俱乐部都在比赛，此条记录必须存在
            $this->paomeDb->update("update r_pm_club_rounds set support_num=support_num+$supportNum,energy_num = energy_num+$energyNum,agree_num=agree_num+$agreeNum where id=$clubRoundId");
        }
        //同步赛事资格表 r_pm_club_status
            $r = $this->paomeDb->update("update r_pm_club_status set support_num=support_num+$supportNum,energy_num = energy_num+$energyNum,agree_num=agree_num+$agreeNum where id=$isInGame");
            if ($r) {
                //同步赛事资格表流水表 r_pm_club_status_list
                $isInGame2 = $this->paomeDb->getOne("select id from r_pm_club_status_list where contest_id=$paomeId and club_id={$arrClubInfo['id']} and `type`=$roundId2 and status=15");//是否在比赛中
                if ($isInGame2) {
                    $this->paomeDb->update("update r_pm_club_status_list set support_num=support_num+$supportNum,energy_num = energy_num+$energyNum,agree_num=agree_num+$agreeNum where id=$isInGame2");
                }

                //人气排名
                $rankClubKeySupportNum = 'rankCLub-supportNum-'.$paomeId.'-'.$roundId;
                $rankClubDayKeySupportNum = 'rankClubDay-supportNum-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$now);//天
                //$rankClubDayKeySupportNumNew = 'rankClubDay-supportNumNew-'.$paomeId.'-'.$roundId.'-'.date('Y-m-d',$now);//天(争霸赛：用于查排名非score)
                $rankClubJdKeySupportNum = 'rankClubJd-supportNum-'.$paomeId.'-'.$roundId.'-'.$roundId2;//阶段

                $this->redis->zIncrBy($rankClubKeySupportNum, $supportNum, $arrClubInfo['id']);
                $this->redis->zIncrBy($rankClubDayKeySupportNum, $supportNum, $arrClubInfo['id']);
                $this->redis->zIncrBy($rankClubJdKeySupportNum, $supportNum, $arrClubInfo['id']);
//                if ($roundId == 100) {
//                    //人气排名重新计算
//                    list($clubCurrentDayMileage, $clubCurrentDayElapsedtime) = $this->getClubCurrentDayMileage($paomeId, $arrClubInfo['id'], date("Ymd", $now));
//                    $dayRunUserNum = $this->getCurrentDayRunUserCnt($paomeId, $arrClubInfo['id'], date("Ymd", $now));
//                    $clubCurrentDaySpeed = 0;
//                    if($dayRunUserNum >=self::CLUB_SPEED_MIN_USER_NUM) {
//                        $clubCurrentDaySpeed = $this->getSecSpeed($clubCurrentDayMileage, $clubCurrentDayElapsedtime);
//                    }
//                    $supportNum = $this->base->load('paome/rank')->getObjectDayRankValue($arrClubInfo['id'], $paomeId, $roundId, date("Ymd", $now), 10);
//                    $newMileRankValue = $this->getFormatSortValue($supportNum, $clubCurrentDaySpeed, 1);
//                    $this->redis->zAdd($rankClubDayKeySupportNumNew, $newMileRankValue,$arrClubInfo['id']);
//                }
            }
        //同步到排名
        $rankId = $this->paomeDb->getOne("select id from r_pm_rank where contest_id=$paomeId and club_id={$arrClubInfo['id']} and `type`=$roundId and type_wd=10 and object_id={$arrClubInfo['id']} and status=0");
        if ($rankId) {
            $this->paomeDb->update("update r_pm_rank set rank_value=rank_value+$supportNum where id=$rankId");
        }  else {
//            $data =[];
//            $data['club_id'] = $arrClubInfo['id'];
//            $data['contest_id'] = $paomeId;
//            $data['type_wd'] = 10;
//            $data['type'] = $roundId;
//            $data['object_id'] = $arrClubInfo['id'];
//            $data['create_time'] = time();
//            $data['status'] = 0;
//            $data['rank_value'] = $supportNum;
            $insertStr = "club_id = {$arrClubInfo['id']},
                            contest_id  = $paomeId,
                            type_wd     = 10,
                            type        = $roundId,
                            object_id   = {$arrClubInfo['id']},
                            status      = 0,
                            create_time = $now,
                            rank_value  = rank_value+$supportNum";

            $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank set {$insertStr} on duplicate key update {$insertStr}");
            //$this->paomeDb->insertRow('r_pm_rank',$data,mysqldb::INSERT_COMMON);
        }
        $formartToday = date('Ymd',time());
        $rankDayId = $this->paomeDb->getOne("select id from r_pm_rank_day where contest_id=$paomeId and club_id={$arrClubInfo['id']} and `type`=$roundId and type_wd=10 and object_id={$arrClubInfo['id']}
                and status=0 and `date`='$formartToday'");
        if ($rankDayId) {
            $this->paomeDb->update("update r_pm_rank_day set rank_value=rank_value+$supportNum where id=$rankDayId");
        }  else {
//            $data =[];
//            $data['club_id'] = $arrClubInfo['id'];
//            $data['contest_id'] = $paomeId;
//            $data['type_wd'] = 10;
//            $data['type'] = $roundId;
//            $data['object_id'] = $arrClubInfo['id'];
//            $data['create_time'] = time();
//            $data['status'] = 0;
//            $data['rank_value'] = $supportNum;
//            $data['date'] = $formartToday;
            $insertStr = "club_id = {$arrClubInfo['id']},
                            contest_id  = $paomeId,
                            type_wd     = 10,
                            type        = $roundId,
                            object_id   = {$arrClubInfo['id']},
                            status      = 0,
                            date        = $formartToday,
                            create_time = $now,
                            rank_value  = rank_value+$supportNum";
            $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_day set {$insertStr} on duplicate key update {$insertStr}");
            //$this->paomeDb->insertRow('r_pm_rank_day',$data,mysqldb::INSERT_COMMON);
        }

        $rankJdId = $this->paomeDb->getOne("select id from r_pm_rank_jd where contest_id=$paomeId and club_id={$arrClubInfo['id']} and `type`=$roundId2 and type_wd=10 and object_id={$arrClubInfo['id']} and status=0");
        if ($rankJdId) {
            $this->paomeDb->update("update r_pm_rank_jd set rank_value=rank_value+$supportNum where id=$rankJdId");
        }  else {
//            $data =[];
//            $data['club_id'] = $arrClubInfo['id'];
//            $data['contest_id'] = $paomeId;
//            $data['type_wd'] = 10;
//            $data['type'] = $roundId2;
//            $data['object_id'] = $arrClubInfo['id'];
//            $data['create_time'] = time();
//            $data['status'] = 0;
//            $data['rank_value'] = $supportNum;
            $insertStr = "club_id     ={$arrClubInfo['id']},
                          contest_id  = $paomeId,
                          type_wd     = 10,
                          type        = $roundId2,
                          object_id   = {$arrClubInfo['id']},
                          status      = 0,
                          create_time = $now,
                          rank_value  = rank_value+$supportNum";
            $this->paomeDb->insert("insert HIGH_PRIORITY into r_pm_rank_jd set {$insertStr} on duplicate key update {$insertStr}");
            //$this->paomeDb->insertRow('r_pm_rank_jd',$data,mysqldb::INSERT_COMMON);
        }
    }

    /**
     * 查询轮次时间
     */
    public function getZbsTime($paomeId) {
        $startTime = 0;
        $endTime = 0;
        $list = $this->paomeDb->getAll("select start_time,type from r_pm_rounds where contest_id = $paomeId and `type` in(105,106)");
        if (!$list) {
            return array($startTime, $endTime);
        }
        foreach ($list as $key=>$val) {
             if ($val['type']== 105) {
                 $startTime = $val['start_time'];
             } else {
                 $endTime = $val['start_time'];
             }
        }
        return array($startTime, $endTime);
    }

    /**
     * 查询用户某天上传的总里程和用时
     * @param $paomeId 赛季ID
     * @param $clubId
     * @param $uid
     * @param $date  日期 eg:20161129
     * @return mixed
     */
    public function getUserCurrentDayMileage($paomeId, $clubId, $uid, $date) {
        $startTimeDid = strtotime(date('Ymd',strtotime($date)));
        $endTimeDid = $startTimeDid+86400;
        $arr = $this->paomeDb->getRow("select sum(`new_mileage`) as totalDayMileage,sum(`new_elapsedtime`) as totalDayElapsedtime from r_pm_club_users_did
                                                          where contest_id=$paomeId
                                                          and club_id=$clubId
                                                          and uid=$uid
                                                          and create_time>=$startTimeDid
                                                          and create_time<$endTimeDid");
        $userCurrentDayMileage     = 0;
        $userCurrentDayElapsedtime = 0;
        if($arr){
            $userCurrentDayMileage     = $arr['totalDayMileage'];
            $userCurrentDayElapsedtime = $arr['totalDayElapsedtime'];
        }
        return array($userCurrentDayMileage, $userCurrentDayElapsedtime);
    }

    /**
     * 查询战队某天的里程和用时
     * @param $paomeId
     * @param $clubId
     * @param $date  日期 eg:20161129
     * @return array
     */
    public function getClubCurrentDayMileage($paomeId, $clubId, $date) {
        $startTimeDid = strtotime(date('Ymd',strtotime($date)));
        $endTimeDid = $startTimeDid+86400;
        $arr = $this->paomeDb->getRow("select sum(`new_mileage`) as totalDayMileage,sum(`new_elapsedtime`) as totalDayElapsedtime
                                                          from r_pm_club_users_did
                                                          where contest_id=$paomeId
                                                          and club_id=$clubId
                                                          and create_time>=$startTimeDid
                                                          and create_time<$endTimeDid");
        $clubCurrentDayMileage     = 0;
        $clubCurrentDayElapsedtime = 0;
        if($arr){
            $clubCurrentDayMileage     = $arr['totalDayMileage'];
            $clubCurrentDayElapsedtime = $arr['totalDayElapsedtime'];
        }
        return array($clubCurrentDayMileage, $clubCurrentDayElapsedtime);
    }

    /**
     * 查询战队当天的跑步人数
     * @param $paomeId
     * @param $clubId
     * @param $date  日期 eg:20161129
     * @return int
     */
    public function getCurrentDayRunUserCnt($paomeId, $clubId, $date) {
        $startTimeDid = strtotime(date('Ymd',strtotime($date)));
        $endTimeDid = $startTimeDid+86400;
        $userNum = $this->paomeDb->getOne("select count(DISTINCT(`uid`))
                                                          from r_pm_club_users_did
                                                          where contest_id=$paomeId
                                                          and club_id=$clubId
                                                          and create_time>=$startTimeDid
                                                          and create_time<$endTimeDid");
        return $userNum;
    }

    /**
     * 查询争霸赛当前展示的日期
     */
    public function getLastDate(array $arrDate) {
        //查询主框显示的日期
        $nowTime = time();
        if(!in_array(date('Ymd', $nowTime), $arrDate)) {
            if(date('Ymd', $nowTime) <= $arrDate[0]){//默认第一天
                $nowTime = strtotime($arrDate[0]);
            } else {
                $nowTime = strtotime(end($arrDate));
            }
        }
        return date('Ymd',$nowTime);
    }

    /**
     * 查询争霸赛子日期
     */
    public function getChildDateList(array $arrDate) {
        //查询主框显示的日期
        $nowTime = time();
        $arrNewDate = [];//子框循环日期
        if(date('Ymd', $nowTime) <= $arrDate[0]){//默认第一天
            $arrNewDate = [];
        } else if(date('Ymd', $nowTime) <= end($arrDate)){
            $nowTime = time();
            for($i=strtotime($arrDate[0]); $i< strtotime(date('Ymd',$nowTime)); $i=$i+86400) {
                $arrNewDate[] = date('Ymd', $i);
            }
        } else {
            array_pop($arrDate);
            $arrNewDate = $arrDate;
        }
        if($arrNewDate) rsort($arrNewDate);
        return $arrNewDate;
    }


    /**
     * 查询争霸赛所有日期
     */
    public function getDateList($paomeId, $roundId=100) {
        $result = [];
        if($roundId == 100) {
           $info = $this->paomeDb->getRow("select start_time,end_time from r_pm_rounds where contest_id=$paomeId and `type` = 105");
            if($info) {
                $startTime = strtotime(date('Ymd', $info['start_time']));
                $endTime = strtotime(date('Ymd', $info['end_time']));
               for($i=$startTime;$i<=$endTime;$i=$i+86400) {
                   $result[] = date('Ymd' ,$i);
               }
            }
        }
        sort($result);
        return $result;
    }

    /**
     * 里程（单位：公里）和 用时（单位：分钟），查询配速（单位：秒）
     * @param $mileage
     * @param $elapsedtime
     * @return string
     */
    public function getSecSpeed($mileage, $elapsedtime) {
        if (!$elapsedtime || !$mileage) return 0;
        $speed_1 = (($elapsedtime/$mileage)/60);//配速
        $num1 = floor($speed_1); //整数部分
        $num2  = $speed_1-$num1;
        $speed = bcadd($num1*60,floor($num2*60));//单位 s/km
        return $speed;
    }

    /**
     * 转换多维度排序
     * @param $objectWd1  里程
     * @param $objectWd2  配速
     * @param int $typeWd  1：里程维度 5配速维度 10 人气维度
     * @return bool|float
     */
    public function getFormatSortValue($objectWd1, $objectWd2, $typeWd=1) {
        $result = false;
        if ($typeWd ==1) {//1 里程/配速
            $speed = $objectWd2 > 0 ? (1/$objectWd2)*0.01 : 0;
            $result =  round($objectWd1*100, 2)+$speed;
        } else if($typeWd == 5){
            $mileage = $objectWd1*0.01;
            $result =  round($objectWd2*100, 2)+$mileage;
        } else if($typeWd == 10) {
            $speed = $objectWd2 > 0 ? (1/$objectWd2)*0.01 : 0;
            $result =  $objectWd1+$speed;
        }
        return $result;
    }

    /**
     * 查询当前轮次和阶段
     * $params $paomeStatus 轮次type r_pm_rounds的type
     */
    public function getCurrentRoundInfo($paomeStatus) {
        $roundId = $this->getRoundId($paomeStatus, 1);
        $roundId2 = $this->getRoundId($paomeStatus, 2);
        return array($roundId, $roundId2);
    }

    /**
     * 查询轮次
     * $params $type 1 轮次 2 轮次阶段
     */
    public function getRoundId($paomeStatus, $type=1) {
        $result = 0;
        if ($type == 1) {
            //轮次
            $arrRounds = array(
                1=>[5,6,10,11],//第一轮
                5=>[15,16,20,21],//第二轮
                10=>[25,26,30,31],//第三轮
                100=>[105,106],//争霸赛
            );
            //赛季轮次
            foreach ($arrRounds as $k=>$v) {
                if (in_array($paomeStatus, $v)) {
                    $result = $k;
                }
            }
        } else {
            //阶段 key 是阶段ID
            $arrRoundsList = array(
                1=>[5,6], //第一轮预选赛
                5=>[10,11], //第一轮决赛
                10=>[15,16], //第二轮预选赛
                15=>[20,21], //第二轮决赛
                20=>[25,26], //第三轮预选赛
                25=>[30,31], //第三轮决赛
                100=>[105,106],//争霸赛
            );
            //赛季当前轮次阶段
            foreach ($arrRoundsList as $k=>$v) {
                if (in_array($paomeStatus, $v)) {
                    $result = $k;
                }
            }
        }
        return $result;
    }
}